Makefile包含 目标文件、依赖文件、可运行命令三部分。

每部分的基本格式例如以下:

test: prog.o  code.o

gcc  -o  test   prog.o   code.o

当中,第一行的test是目标文件。 prog.o、code.o是依赖文件

第二行的gcc -o test prog.o code.o是可运行命令

整个Makefile文件都是这样的格式。

下面是一些example:

-----------------------Makefile example 1----------------------------------

#this line is the comment for the Makefile

test: prog.o code.o

gcc   prog.o  code.o  -o test

prog.o: prog.c  prog.h  code.h

gcc  -c  prog.c  -o  prog.o

code.o: code.c  code.h

gcc  -c  code.c  -o  code.o

clean:

rm  -f  *.o

----------------------------------------------------------------------------------------------

-------------------------------example 2(包括变量)---------------------------------

#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

CFLAGS = -Wall -g -O

test: ${OBJS}

${CC}  ${CFLAGS}  ${OBJS}  -o test

prog.o: prog.c  prog.h  code.h

${CC}  ${CFLAGS}  -c prog.c  -o prog.o

code.o: code.c  code.h

${CC}   ${CFLAGS}  -c code.c  -o code.o

clean:

rm  -f  *.o

------------------------------------------------------------------------------------------------


-------------------------------example 3(使用Makefile的隐含规则)---------------------------------

1,假设没有对应的编译命令,则使用隐含规则,全部的 ".c文件" 编译成与它名称同样的 ".o文件"。

2, 使用Makefile的自己主动变量。

#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

test: ${OBJS}

${CC}   -o  $@   $^

prog.o: prog.c  prog.h  code.h             #no exec command,and will generate the prog.o

code.o: code.c  code.h                        #no exec command,and will generate the code.o

clean:

rm  -f  *.o

------------------------------------------------------------------------------------------------

-----------------------------------------------------下面为我測试过的实例文件内容 :-----------------------------------------------------

========================== Makefile ===============

#this line is the comment

CC = gcc

OBJS = my_str.o

CFLAGS = -Wall -g -O





program: my_main.c ${OBJS}

               ${CC} ${CFLAGS} $^ -o $@ 

my_str.o: my_str.c my_str.h

               ${CC} ${CFLAGS} -c my_str.c -o my_str.o





clean:

               rm -f *.o

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

==========================my_main.c=======================

#include <unistd.h>

#include <stdlib.h>

#include "my_str.h"



int 

main(int argc, const char **argv)

{

if(my_cmp(argv[1], argv[2]) == 0)

write(1, "Equal !\n", sizeof("Equal !\n"));

else

write(1, "Not Equal !\n", sizeof("Not Equal !\n"));





exit(0);

}

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

========================my_str.c=============================

#include "my_str.h"





int 

my_cmp(const char *str1, const char *str2)

{

if(!str1 || !str2)

return -1;

while(*str1 && *str2 && *str1 == *str2)

str1++, str2++;

return *str1 - *str2;

}

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

===================my_str.h============================================

#ifndef _MY_STR_H

#define _MY_STR_H





int my_cmp(const char *str1, const char *str2);





#endif

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

最新文章

  1. 毫秒级的时间处理上G的图片(生成缩略图)
  2. 转:使用DBUnit测试时违反外键约束的解决办法
  3. [DFNews] 入侵汽车控制刹车和油门?——速度与激情6 的节奏?
  4. Java static解析
  5. XJOI网上同步测试DAY14 T3
  6. C++0x简讯
  7. 1.2低级线程处理API
  8. [LeetCode] Wiggle Sort II 摆动排序之二
  9. JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法
  10. 数据库--&gt;表操作
  11. jQuery.Form.js 异步提交表单使用总结
  12. how2j 的shiro教程初探
  13. java必背面试题
  14. 删除N天前的备份文件脚本(windows)
  15. 自测之Lesson12:信号量
  16. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释
  17. 洛谷P4004 Hello world!(分块+并查集)
  18. sublime text 3 使用技巧
  19. Sublime Text 3显示文本编码
  20. 前端手势控制图片插件书写四(图片上传及Ios图片方向问题)

热门文章

  1. Myeclipse学习总结(4)——Eclipse常用开发插件
  2. COGS——T 1215. [Tyvj Aug11] 冗余电网
  3. Class C++
  4. 维护的JSP站点数据丢失
  5. 常见的C字符串处理函数的源代码
  6. node --- 服务一直启动
  7. sql阻塞进程查询
  8. 《Linux企业应用案例精解》一书已由清华大学出版社出版
  9. Sandbox
  10. CMDB学习之七-实现采集错误捕捉,日志信息处理