专栏中心

EEPW首页 > 专栏 > ****学Makefile

****学Makefile

发布人:xiajiashan 时间:2012-08-29 来源:工程师 发布文章
(第一部分:自己编写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

专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们

关键词:

相关推荐

并口仿真器WIGGLER的原理图

RP5底盘改装智能车机器人作品01

视频 2010-02-01

[求助]USB

silvercx 2004-12-10

USB BLASTER制作资料

layout中电源和地的处理方法

感谢DeepSeek,春节后中国AI创企融资井喷

2009年第四届智能车竞赛北京科技大学表演车模(光电组)

视频 2010-02-01

ASURO智能车功能检测演示

视频 2010-02-01

联发科发布Genio 720/520智能物联网芯片,支持生成式AI模型

英特尔迎来新掌门,陈立武能否力挽狂澜

Sandisk闪迪携UFS 4.1存储解决方案亮相CFMS

罗兰贝格《蓄势而行:2040年全球汽车行业前景展望》重磅发布

全球采用RTI Connext Drive软件框架上路行驶汽车超过100万辆

HDMI 1.3规范

Solidigm高密度方案解决数据中心存储难题

ASURO智能车走迷宫功能演示

视频 2010-02-01

自制Multi-ICE仿真器资料

Microchip推出集成高性能模拟外设的32位PIC32A单片机

ASURO循迹避障测速智能车平衡功能演示

视频 2010-02-01

长安汽车回应阿维塔科技拟赴港上市消息

截胡苹果高通!曝三星Exynos 2600原型五月量产:全球首款2nm芯片

更多 培训课堂
更多 焦点
更多 视频

技术专区