Makefile的基本语法命令

注:文章供本人学习用       链接 http://blog.csdn.net/jundic/article/details/17535445

一、Makefile 中的赋值方法

=    递归赋值 可以向后引用变量

:= 简单扩展 只能引用前面的变量

?= 如果没有赋值 则赋值一次

+=  在原来的基础上添加赋值变量

一个例子说明

[plain]  view plain  copy
  1. x = before  
  2. y = $(x)  
  3. x = later  
  4.   
  5. xx = before  
  6. yy := $(xx)  
  7. xx = later  
  8.   
  9. xxx = before  
  10. xxx ?= later  
  11.   
  12. yyy ?= before  
  13. yyy = later  
  14.   
  15. x += last  
  16.   
  17. all :  
  18.     @echo "x=" $(x)  
  19.     @echo "y=" $(y)  
  20.     @echo "xx=" $(xx)  
  21.     @echo "yy=" $(yy)  
  22.     @echo "xxx=" $(xxx)  
  23.     @echo "yyy=" $(yyy)  

输出是

 x= later last

y= later last

xx=later

yy=before

xxx=before

yyy=later

二、Makefile 自动变量

$@   目标

$<    所以的依赖文件

$^     第一个依赖文件

[plain]  view plain  copy
  1. test.o : test1.c test2.c  
  2.           @echo $@  
  3.           @echo $<  
  4.           @echo $^  

结果为

test.o

test1.c test2.c

test1.c

三、Makefile中的常用函数

1、文本替换 函数 $(subst ma,   wo   ,hello world!!)

将hello world!! 中间的wo 替换成ma

结果为  hello marld!!

 

2、格式替换函数 $(patsubst %.c ,%.o , test.c test1.c test2.c)

将.c 的格式替换成.o

结果为 test.o test1.o test2.o

 

3、去掉空格函数 $(strip a    b               c)

结果 a b c

4、字符串查找函数

$(findstring a,a b c d)

在字符串a b c d 中查找 a  ,找到就返回查到的值 ,否则为 "  "

上面结果为 a

 

5、格式匹配过滤函数 $(filter %.c %.s , mod1.c mod2.o mod3.s mod4.h)

查找格式是%.c 和 %.s 的文件

结果为 mod1.c mod2.s

 

6、格式不匹配函数 $(filter-out %.c %.s, mod1.c mod2.o mod3.s mod4.h)

它的和filter相反

结果为 mod2.o mod4.h

 

7、$(sort  hello  world branck)

按字母排列

结果为  branck hello world

 

8 、抽取文件目录函数$( dir source/hello.c inc/hello.h mksh)

结果: source/  inc/  ./

 

9、抽取文件名函数 $(nodir source/hello.c inc/hello.h mksh)

结果: hello.c hello.h mksh

 

10、提取文件名后缀函数 $(suffix source/hello.c inc/hello.h mksh)

结果: .c  .h

 

11、去除后缀名函数 $(basename sourc/hello.c inc/hello.h mksh)

结果: source/hello   inc/hello mksh

 

12、添加后缀名函数 $(addsuffix .c ,hello fun)

结果: hello.c fun.c

 

13、添加前缀函数 $(addprefix source/ , mod1.c mod2.c mod3.c)

结果: source/mod1.c source/mod2.c source/mod3.c

 

14、格式匹配函数 $(wildcard  source/*.c)

查找source 目录下所有.c 文件

 

15、格式匹配用法

SRCS = mod1.c mod2.c mod3.c

$(SRCS : %.c = %.o)

结果为mod1.o mod2.o mod3.o

 

16、目录循环搜索

dirs :=  dir1 dir2 dir3 dir4

files := $( foreach dir ,$(dirs) ,$(wildcard $(dir)/*)

查找 目录 dir1 dir2 dir3 dir4 目录下所有的文件。

 

17、在Makefile 中执行shell 脚本的方法

$(shell command)

SRCS := $(shell ls *.c)

则srcs 中所有的c程序。

 

四、Makefile 中的编译基础知识

1、编译时 指定头文件路径 用 -I

2、编译时 指定库路径用 -L 指定函数库名 用 -l (小写L )

3、在Makefile 指定宏定义 配置到程序中 -D

 

五、条件判断语句

1、ifdef/ifndf  (param)

       endif    

#    param  是否定义

2、ifeq/ifneq (a,b)

       endif  

#  a和b是否相等


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值