编写简单Makefile (一)

 
Makefile 编写简单(一)
--单一的C工程
       编写一个自己工程需要的Makefile其实很简单,以下是一个俺的一个实例,包含了中英文注释,小看一下,应该就很快明白了,要求不多的话,一个Makefile几句就能搞定,详细请查看Gcc参数设置和Makefile规则。
1.       工程文件的分布
./
Makefile
-------
src (文件夹)
test2.c
test.c
include
test.h
2.       文件内容
Test.h
#include <stdio.h>
void testPrint();
test.c
#include "test.h"
int main()
{
       testPrint();
       return 1;
}
test2.c
#include "test.h"
void testPrint()
{
       printf("************************/n");
       printf("dir1--------test/n");
       printf("************************/n");
}
Makefile
###########################################################################
####              created by zsjum -- 07.12.11                               ####
#####                                                                 ######
###########################################################################
#编译工具
#compile tool
CC = gcc
 
#存放头文件路径
#the path of the head files
INCLUDE_DIR=-Iinclude -I../lib/include -I../so/include
 
#加载库 -L 加载路径 -l 加载库的名称
#load lib    -L load path of the lib ; -l load name of the lib
LIBS=-l pthread -l dl
 
#目标工程命名
#name the target project
TARGET=test
 
# 编译器设置 (-wall 输出警告信息; -O 编译时进行优化),请参看GCC 参数设置
# compile option(-wall output warning message; -O optimize compile)
CFLAGS = -Wall -O3
 
# 获取本地 .c 后缀所有文件列表并赋值变量SOURCE_C
# get all files name is *.c to SOURCE_C
SOURCE_C=$(wildcard src/*.c)
 
#将变量SOUTCE_C的所有*.c改成*.o,付直给变量OBJECT_O
#replace *.c to *.o from variable SOUTCE_C, and get the name string to variable OBJECT_O
OBJECT_O=$(SOURCE_C:.c=.o)
 
#下面是"隐含规则"生成函数库打包文件(也可以用"后缀规则"生成, 请参考详细Makefile文档)
#%.o表明所有以“.o”后缀的目标集, 如 "test1.o test2.o"
#依赖模式"%.c"就是取模式“%.o”的“%”,并加上".c"的后缀,如 "test1.c test2.c" 
#"$<"是指所有的依赖目标集,如 "test1.c test2.c"
#"$@"是指所表示目标 集,如 "test1.o test2.o" 
#参数 -c:编译但不连接; -I:头文件路径; -o:输出 (参照 gcc --help)
#Compile and assemble, but do not link
 
#.c.o:
%.o: %.c
       $(CC) -c $(CFLAGS) $(INCLUDE_DIR) $< -o $@  
 
#$(CC) $(CFLAGS) $(MENUINC) -c $< -o $@
 
 
$(TARGET): $(OBJECT_O)
#      $(CC) $(OBJECT_O) -o $(TARGET)
       $(CC) ${CFLAGS}   $(OBJECT_O) $(LIBS) -o $@
       @echo "********************"
       @echo "******success*******"
       @echo "********************"
 
clean:
       rm test src/*.o -rf
       @echo "********************"
       @echo "******success*******"
       @echo "********************"
Makefile 编写简单(二)静态库,待续… 有错的地方请指教 :-)
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值