Linux下Makefile编写语法

原文链接: https://blog.csdn.net/u014646950/article/details/52061020

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 ) f o o = (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

————————————————
版权声明:本文为CSDN博主「Datrilla」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014646950/article/details/52061020

猜你喜欢

转载自blog.csdn.net/changdizm/article/details/102648440
今日推荐