Linux下Makefile编写语法

Makefile样例:http://blog.chinaunix.net/uid-25100840-id-2047826.html

all : main.c foo1.c foo2.c foo3.c
        gcc main.c foo1.c foo2.c foo3.c -o all

targets:prerequisites
        command
或者
targets :prerequisites
        command

targets:目标文件名可以多个,空格隔开
prerequisite:依赖目标
command:命令行,命令太长用反斜杠\作为换行符,需要Tab作为开头

 

 

%代表任意长度串,空格不属于

 

 

 

 

在Makefile中使用命令要Tab开始,

否则会出现“遗漏分隔符,停止”。

变量有(=)和(:=)两种,前者会造成递归定义。

(?=)如果左边变量未定义则赋予右值。

变量使用需要$(变量)或者${变量}。

全局变量

 

 

 

define 变量
        命令
endef

全局变量

 

 

 

override 指示符

全局变量

 

 

 

 $@目标集合 $<依赖目标集合
     自动化变量 中$<属于规则性变量,依赖于规则的目标和依赖目标的定义

目标变量

自动化变量运行才有值

 

 

%.o :CFLAGS=-o

目标模式变量

 

 

 

$(function argument1,argument2)
${function argument1,argument2}

函数调用

 

 

 

若干个单词串处理函数

$(subst replaceword,newword,sourceText

 

$(subst ee,EE,feet on the street)

fEEt on the strEEt

空返回‘ ’

$(patsubst pattern,replacement,sourcetext)

 

$(patsubst %.c,%.o,x.c.c bar.c)

x.c.o bar.o

 

$(findstring find,sourcetext)

有返回对应,否则' '

 

 

 

$(filter pattern1 pattern2 ,sourceText)

过滤器,返回SourceText符合 %.后缀的模式

 

 

 

$(filter-out pattern1 pattern2,sourceText)

反过滤,返回不符合对应模式

 

 

 

$(sort word1 word2)

字母序升序排序且去重

 

 

 

$(word index_1,sourceText)

取第index个单词(单词不是字符),从1开始

 

 

 

$(wordlist fromIndex_1,toIndex_1,sourceText)

去从fromIndex到toIndex的文本单词

 

 

 

$(words sourceText)

统计单词个数

 

 

 

$(firstword sourceText)

返回首单词

 

 

文件名操作函数

$(dir  name1 name2)

取目录函数,返回文件所在目录,不包括文件名

$(dir usr/e/hha.c dd)

usr/e/  ./

 

$(notdir name1 name2)

取文件名

$(notdir usr/e/hh.c dd)

hh.c dd

 

$(suffix name1 name2)

取后缀,无后缀‘ ’

 

 

 

$(basename name1 name2)

取前缀,无前缀‘ ’

 

 

 

$(addsuffix suffix,sourceText)

加后缀

 

 

 

$(addprefix prefix,sourceText)

加前缀

 

 

 

$(join list1,list2)

连接函数,对应index的连接,无对应自己

 

 

 

$(foreach oneOfList,list,EveryDealText)

循环函数,list中取之局部变量oneOfList中,用到处理部分,每个返回空格连接

 

 

条件判断函数

ifeq (arg1,arg2)  endif

if equal 判断是否相等?真:假;

 

 

 

ifneq (arg1,arg2)  endif

判断是否不等?真:假;

 

 

 

ifdef variable-name  endif

变量有值?真:假;

 

 

 

ifndef variable-name  endif

变量空值?真:假;

 

 

 

$(if condition,then_part,else_part)

 

 

 

 

$(if condition,then_part)

 

 

 

其他函数

$(call expression,parm1,parm2)

在expression中用到后面的parm1用$(1)

reverse=$(2) $(2)
foo=$(call reverse,a,b)

b a

 

$(origin variable)

Undefined未定义
Dafault默认定义 CC
environment环境变量
file被定义在Makefile
command line被命令行定义
override指示符重新定义
automatic一个命令运行中的自动化变量

 

 

 

$(shell shell命令)

生成一个shell程序来执行命令

$(shell echo just text)

just text

控制make的函数

$(error text)

 

ifdef ERROR_001
$(error error is $(ERROR_001))
endif

 

 

$(warning text)

 

 

 

模式规则

destiPattern:sourcePattern;command

%.o:%.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

 

 

@echo just text 

echo just text
just text

 

 

 

echo just text

just text

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值