打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
深入浅出学Makefile<一:自己写Makefile>

(第一部分:自己编写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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
scp命令详解
linux下文件的特殊权限s和t
学霸Linux基础命令吐血总结,给你当新华字典用
Linux文件属性、权限设置
如何使用特殊权限:setuid、setgid 和 sticky 位
[转载] Linux的capability深入分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服