新闻  |   论坛  |   博客  |   在线研讨会
****学Makefile
xiajiashan | 2012-08-29 10:33:48    阅读:676   发布文章

(第一部分:自己编写makefile )

整理:下家山

前言:

Makefile其中奥妙无穷,足可以让一个人当作一辈子的工作去做!但,我们仅仅是应用,所以我们的目标是能读懂,并且会写一些简单的makefile。很多东西不必深究!!!

一:编辑.c源文件

我们先用vi建立三个.c文件,分别为a.c,b.c,c.c

root@parson-desktop:/home/parson/tmp/mk_test# ls

root@parson-desktop:/home/parson/tmp/mk_test# vi a.c

 

#include

 

void printa(void)

{

printf("call function %s\n",__FUNCTION__);

}

~

~

~

~

"a.c" [New File] 5 lines, 85 characters written

root@parson-desktop:/home/parson/tmp/mk_test#

把a.c拷贝成b.c c.c

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c

root@parson-desktop:/home/parson/tmp/mk_test# cp a.c b.c

root@parson-desktop:/home/parson/tmp/mk_test# cp a.c c.c

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c

root@parson-desktop:/home/parson/tmp/mk_test#

利用sed替换b.c中printa成printb;

利用sed替换c.c中printa成printc;

root@parson-desktop:/home/parson/tmp/mk_test# sed -i "s/printa/printb/" b.c

root@parson-desktop:/home/parson/tmp/mk_test# cat b.c

#include

void printb(void)

{

printf("call function %s\n",__FUNCTION__);

}

root@parson-desktop:/home/parson/tmp/mk_test# sed -i "s/printa/printc/" c.c

root@parson-desktop:/home/parson/tmp/mk_test# cat c.c

#include

void printc(void)

{

printf("call function %s\n",__FUNCTION__);

}

root@parson-desktop:/home/parson/tmp/mk_test#

再写一个主文件m.c来调用其他三个文件

root@parson-desktop:/home/parson/tmp/mk_test# vi m.c

#include

 

void printa(void);

void printb(void);

void printc(void);

 

int main(void)

{

printa();

printb();

printc();

 

return 0;

}

~

"m.c" [New File] 14 lines, 178 characters written

root@parson-desktop:/home/parson/tmp/mk_test#

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m.c

=======================================================================================

By 下家山 Q群 75303301 上海松江文汇路928号258室 松江大学城

上海索漫科技 http://www.xiajiashan.com 专注嵌入式(ARM7,Cortex-M0,Cortex-M3,ARM9,linux)培训

二:不用makefile编译

所有文件已建立,编译所有文件,生成可执行文件m

root@parson-desktop:/home/parson/tmp/mk_test# gcc -o m *.c

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m m.c

可执行文件m已得到,运行结果

root@parson-desktop:/home/parson/tmp/mk_test# ./m

call function printa

call function printb

call function printc

root@parson-desktop:/home/parson/tmp/mk_test#

三:简单的makefile如此简单

root@parson-desktop:/home/parson/tmp/mk_test# vi makefile

 

test:

gcc -o $@ *c

~

~

~ "makefile" [New File] 2 lines, 20 characters written

其中$@即代表目标test,参考徐海滨写的makefile

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m makefile m.c

root@parson-desktop:/home/parson/tmp/mk_test# cat makefile

test:

gcc -o $@ *c

root@parson-desktop:/home/parson/tmp/mk_test# make

gcc -o test *c

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m makefile m.c test

root@parson-desktop:/home/parson/tmp/mk_test# ./test

call function printa

call function printb

call function printc

加入依赖关系

root@parson-desktop:/home/parson/tmp/mk_test# vi makefile

=======================================================================================

By 下家山 Q群 75303301 上海松江文汇路928号258室 松江大学城

上海索漫科技 http://www.xiajiashan.com 专注嵌入式(ARM7,Cortex-M0,Cortex-M3,ARM9,linux)培训

OBJS=a.o b.o c.o m.o

 

test:$(OBJS)

gcc -o $@ *.c

~

~

~

~

"makefile" 5 lines, 51 characters written

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m makefile m.c test

root@parson-desktop:/home/parson/tmp/mk_test# rm test

root@parson-desktop:/home/parson/tmp/mk_test# make

cc -c -o a.o a.c

cc -c -o b.o b.c

cc -c -o c.o c.c

cc -c -o m.o m.c

gcc -o test *.c

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c a.o b.c b.o c.c c.o m makefile m.c m.o test

root@parson-desktop:/home/parson/tmp/mk_test# ./test

call function printa

call function printb

call function printc

root@parson-desktop:/home/parson/tmp/mk_test#

加入伪目标clean

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c a.o b.c b.o c.c c.o m makefile m.c m.o test

root@parson-desktop:/home/parson/tmp/mk_test# vi makefile

 

 

OBJS=a.o b.o c.o m.o

 

test:$(OBJS)

gcc -o $@ *.c

 

clean:

rm -f *.o test

~

~

~

~

"makefile" 8 lines, 75 characters written

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c a.o b.c b.o c.c c.o m makefile m.c m.o test

root@parson-desktop:/home/parson/tmp/mk_test# make clean

rm -f *.o test

root@parson-desktop:/home/parson/tmp/mk_test# ls

a.c b.c c.c m makefile m.c

root@parson-desktop:/home/parson/tmp/mk_test#

写于上海松江 作者:下家山(请尊重原创, 转载请注明) http://www.xiajiashan.com,有什么问题可与我联系:ximenpiaoxue4016@sina.com

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
02年接触ARM和ucos,开发过有线和无线图像报警器,IPCamera,人脸识别系统,OCR识别系统,指纹识别系统,05年开始从事Linux及Rtems下WiFi,camera,Ethernet等驱动开发工作,专做嵌入式linux培训,致力于把我十年来的研发经验传授给每一个学员,招人的可以找我,ximenpiaoxue4016@sina.com
推荐文章
最近访客