简单的makefile编写

来源:互联网 发布:家庭电子记账软件 编辑:程序博客网 时间:2024/05/30 04:26

首先我们给定三个文件:main_plus.c, plus.c plus.h

main_plus.c

/*************************************************************************> File Name: main_plus.c> Author: ahuang1900> Mail: ahuang1900@qq.com > Created Time: 2014年10月04日 星期六 20时27分53秒 ************************************************************************/#include<stdio.h>#include "plus.h"int main(void){int a = 0, b = 0;printf("Please enter integer a:");scanf("%d",&a);printf("\nPlease enter integer b:");scanf("%d",&b);if ( a>b) {printf("\nThe sum is %d\n", plus(b,a));} else {printf("\nThe sum is %d\n", plus(a,b));}return 0;}

plus.c

/*************************************************************************> File Name: plus.c> Author: ahuang1900> Mail: ahuang1900@qq.com > Created Time: 2014年10月04日 星期六 20时34分18秒 ************************************************************************/#include<stdio.h>#include "plus.h"int plus(int a, int b){int sum = a;int i;for (i = a+1; i<=b; i++)sum += i;return sum;}

plus.h

/*************************************************************************> File Name: plus.h> Author: ahuang1900> Mail: ahuang1900@qq.com > Created Time: 2014年10月04日 星期六 20时32分02秒 ************************************************************************/#ifndef _PLUS_H_#define _PLUS_H_#include <stdio.h>int plus(int a, int b);#endif


如果不编写makefile,通常我们的做法是:

hellen@hellen1900:~/myjob/makefile$ lsmain_plus.c  makefile  plus.c  plus.hhellen@hellen1900:~/myjob/makefile$ gcc -c main_plus.chellen@hellen1900:~/myjob/makefile$ gcc -c plus.chellen@hellen1900:~/myjob/makefile$ lsmain_plus.c  main_plus.o  makefile  plus.c  plus.h  plus.ohellen@hellen1900:~/myjob/makefile$ gcc -o main plus.o main_plus.ohellen@hellen1900:~/myjob/makefile$ lsmain  main_plus.c  main_plus.o  makefile  plus.c  plus.h  plus.ohellen@hellen1900:~/myjob/makefile$ ./mainPlease enter integer a:1Please enter integer b:2The sum is 3hellen@hellen1900:~/myjob/makefile$ 

因此我们可以编写makefile如下:

版本1:

main: main_plus.o plus.ogcc -o main main_plus.o plus.omain_plus.o: main_plus.c plus.hgcc -c main_plus.cplus.o: plus.c plus.hclean:rm -f *.o main

版本2:

#$@:目标文件#$^:所有的依赖文件#$<:第一个依赖文件main:main_plus.o plus.ogcc -o $@ $^main_plus.o: main_plus.c plus.hgcc -c $<plus.o: plus.c plus.hgcc -c $<clean:rm -f *.o main


版本3:

object = main_plus.o plus.omain: $(object)gcc -o $@ $(object)main_plus.o: main_plus.cplus.o: plus.cclean:rm -f *.o main

版本4:

CC=gccCFLAG = -g -Wallobject = main_plus.o plus.omain : $(object)$(CC) $(CFLAG) -o $@ $^main_plus.o : main_plus.cplus.o : plus.c%.o:%c$(CC) $(CFLAG) -c -o $@ $<clean:rm -f *.o main


参考:http://blog.csdn.net/xj626852095/article/details/37879217


0 0
原创粉丝点击