ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux 的make及makefile文件格式,android面试项目经验

2021-12-17 19:04:44  阅读:142  来源: 互联网

标签:foo 命令 make makefile echo source 文件格式 txt


$ make clean

但是,如果当前目录中,正好有一个文件叫做clean,那么这个命令不会执行。因为Make发现clean文件已经存在,就认为没有必要重新构建了,就不会执行指定的rm命令。

为了避免这种情况,可以明确声明clean是"伪目标",写法如下。

.PHONY: clean

clean:

    rm <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">\*</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>o temp

声明clean是"伪目标"之后,make就不会去检查是否存在一个叫做clean的文件,而是每次运行都执行对应的命令。像.PHONY这样的内置目标名还有不少,可以查看手册

如果Make命令运行时没有指定目标,默认会执行Makefile文件的第一个目标。

$ make

上面代码执行Makefile文件的第一个目标。

2.3 前置条件(prerequisites)

前置条件通常是一组文件名,之间用空格分隔。它指定了"目标"是否重新构建的判断标准:只要有一个前置文件不存在,或者有过更新(前置文件的last-modification时间戳比目标的时间戳新),"目标"就需要重新构建。

result.txt: source.txt

cp source<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt result<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

上面代码中,构建 result.txt 的前置条件是 source.txt 。如果当前目录中,source.txt 已经存在,那么make result.txt可以正常运行,否则必须再写一条规则,来生成 source.txt 。

source.txt:

<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"this is the source"</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">></span> source<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

上面代码中,source.txt后面没有前置条件,就意味着它跟其他文件都无关,只要这个文件还不存在,每次调用make source.txt,它都会生成。

$ make result.txt

$ make result.txt

上面命令连续执行两次make result.txt。第一次执行会先新建 source.txt,然后再新建 result.txt。第二次执行,Make发现 source.txt 没有变动(时间戳晚于 result.txt),就不会执行任何操作,result.txt 也不会重新生成。

如果需要生成多个文件,往往采用下面的写法。

source: file1 file2 file3

上面代码中,source 是一个伪目标,只有三个前置文件,没有任何对应的命令。

$ make source

执行make source命令后,就会一次性生成 file1,file2,file3 三个文件。这比下面的写法要方便很多。

$ make file1

$ make file2

$ make file3

2.4 命令(commands)

命令(commands)表示如何更新目标文件,由一行或多行的Shell命令组成。它是构建"目标"的具体指令,它的运行结果通常就是生成目标文件。

每行命令之前必须有一个tab键。如果想用其他键,可以用内置变量.RECIPEPREFIX声明。

.RECIPEPREFIX = >

all:

> echo Hello, world

上面代码用.RECIPEPREFIX指定,大于号(>)替代tab键。所以,每一行命令的起首变成了大于号,而不是tab键。

需要注意的是,每行命令在一个单独的shell中执行。这些Shell之间没有继承关系。

var-lost:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

上面代码执行后(make var-lost),取不到foo的值。因为两行命令在两个不同的进程执行。一个解决办法是将两行命令写在一行,中间用分号分隔。

var-kept:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

另一个解决办法是在换行符前加反斜杠转义。

var-kept:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

最后一个方法是加上.ONESHELL:命令。

.ONESHELL:

var-kept:

export foo<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">=</span>bar<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> 
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token string" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(102, 153, 0);">"foo=\[$<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$foo</span>\]"</span>

三、Makefile文件的语法


3.1 注释

井号(#)在Makefile中表示注释。

# 这是注释

result.txt: source.txt

<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># 这是注释

cp source.txt result.txt # 这也是注释

3.2 回声(echoing)

正常情况下,make会打印每条命令,然后再执行,这就叫做回声(echoing)。

test:

<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># 这是测试

执行上面的规则,会得到下面的结果。

$ make test

# 这是测试

在命令的前面加上@,就可以关闭回声。

test:

@<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># 这是测试

现在再执行make test,就不会有任何输出。

由于在构建过程中,需要了解当前在执行哪条命令,所以通常只在注释和纯显示的echo命令前面加上@。

test:

@<span class="token comment" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(112, 128, 144);"># 这是测试

@echo TODO

3.3 通配符

通配符(wildcard)用来指定一组符合条件的文件名。Makefile 的通配符与 Bash 一致,主要有星号(*)、问号(?)和 […] 。比如, *.o 表示所有后缀名为o的文件。

clean:

    rm <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>f <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">\*</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>o

3.4 模式匹配

Make命令允许对文件名,进行类似正则运算的匹配,主要用到的匹配符是%。比如,假定当前目录下有 f1.c 和 f2.c 两个源码文件,需要将它们编译为对应的对象文件。

%.o: %.c

等同于下面的写法。

f1.o: f1<span class=“token punctuation” style="margin: 0px; paddin

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

g: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.c

f2.o: f2.c

使用匹配符%,可以将大量同类型的文件,只用一条规则就完成构建。

3.5 变量和赋值符

Makefile 允许使用等号自定义变量。

txt = Hello World

test:

@<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>txt<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span>

上面代码中,变量 txt 等于 Hello World。调用时,变量需要放在 $( ) 之中。

调用Shell变量,需要在美元符号前,再加一个美元符号,这是因为Make命令会对美元符号转义。

test:

@<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$HOME</span>

有时,变量的值可能指向另一个变量。

v1 = $(v2)

上面代码中,变量 v1 的值是另一个变量 v2。这时会产生一个问题,v1 的值到底在定义时扩展(静态扩展),还是在运行时扩展(动态扩展)?如果 v2 的值是动态的,这两种扩展方式的结果可能会差异很大。

为了解决类似问题,Makefile一共提供了四个赋值运算符 (=、:=、?=、+=),它们的区别请看StackOverflow

VARIABLE = value

# 在执行时扩展,允许递归扩展。

VARIABLE := value

# 在定义时扩展。

VARIABLE ?= value

# 只有在该变量为空时才设置值。

VARIABLE += value

# 将值追加到变量的尾端。

3.6 内置变量(Implicit Variables)

Make命令提供一系列内置变量,比如, ( C C ) 指 向 当 前 使 用 的 编 译 器 , (CC) 指向当前使用的编译器, (CC)指向当前使用的编译器,(MAKE) 指向当前使用的Make工具。这主要是为了跨平台的兼容性,详细的内置变量清单见手册

output:

$<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>CC<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>o output input<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>c

3.7 自动变量(Automatic Variables)

Make命令还提供一些自动变量,它们的值与当前规则有关。主要有以下几个。

(1)$@

$@指代当前目标,就是Make命令当前构建的那个目标。比如,make foo的 $@ 就指代foo。

a.txt b.txt:

touch <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span>

等同于下面的写法。

a.txt:

touch a<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

b.txt:

touch b<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt

(2)$<

< 指 代 第 一 个 前 置 条 件 。 比 如 , 规 则 为 t : p 1 p 2 , 那 么 < 指代第一个前置条件。比如,规则为 t: p1 p2,那么 <指代第一个前置条件。比如,规则为t:p1p2,那么< 就指代p1。

a.txt: b.txt c.txt

cp $<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);"><</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span> 

等同于下面的写法。

a.txt: b.txt c.txt

cp b<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt a<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">.</span>txt 

(3)$?

? 指 代 比 目 标 更 新 的 所 有 前 置 条 件 , 之 间 以 空 格 分 隔 。 比 如 , 规 则 为 t : p 1 p 2 , 其 中 p 2 的 时 间 戳 比 t 新 , ? 指代比目标更新的所有前置条件,之间以空格分隔。比如,规则为 t: p1 p2,其中 p2 的时间戳比 t 新, ?指代比目标更新的所有前置条件,之间以空格分隔。比如,规则为t:p1p2,其中p2的时间戳比t新,?就指代p2。

(4)$^

$^ 指代所有前置条件,之间以空格分隔。比如,规则为 t: p1 p2,那么 $^ 就指代 p1 p2 。

(5)$*

KaTeX parse error: Undefined control sequence: \* at position 1: \̲*̲ 指代匹配符 % 匹配的部分,…* 就表示 f1。

(6)$(@D) 和 $(@F)

$(@D) 和 $(@F) 分别指向 @ 的 目 录 名 和 文 件 名 。 比 如 , @ 的目录名和文件名。比如, @的目录名和文件名。比如,@是 src/input.c,那么 ( @ D ) 的 值 为 s r c , (@D) 的值为 src , (@D)的值为src,(@F) 的值为 input.c。

(7)$(<D) 和 $(<F)

$(<D) 和 $(<F) 分别指向 $< 的目录名和文件名。

所有的自动变量清单,请看手册。下面是自动变量的一个例子。

dest/%.txt: src/%.txt

@<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">\[</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">-</span>d dest <span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">\]</span> <span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);">||</span> mkdir dest
cp $<span class="token operator" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(166, 127, 89);"><</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$@</span>

上面代码将 src 目录下的 txt 文件,拷贝到 dest 目录下。首先判断 dest 目录是否存在,如果不存在就新建,然后,$< 指代前置文件(src/%.txt), $@ 指代目标文件(dest/%.txt)。

3.8 判断和循环

Makefile使用 Bash 语法,完成判断和循环。

ifeq ($(CC),gcc)

libs=$(libs_for_gcc)

else

libs=$(normal_libs)

endif

上面代码判断当前编译器是否 gcc ,然后指定不同的库文件。

LIST = one two three

all:

<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">for</span> i <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">in</span> $<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">(</span>LIST<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">)</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">do</span> \\
    <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> $<span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$i</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">done</span>

# 等同于

all:

<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">for</span> i <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">in</span> one two three<span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">do</span> \\
    <span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">echo</span> <span class="token property" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 0, 85);">$i</span><span class="token punctuation" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(153, 153, 153);">;</span> \\
<span class="token keyword" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(0, 119, 170);">done</span>

上面代码的运行结果。

one

two

three

3.9 函数

Makefile 还可以使用函数,格式如下。

$(function arguments)

# 或者

${function arguments}

Makefile提供了许多内置函数,可供调用。下面是几个常用的内置函数。

(1)shell 函数

shell 函数用来执行 shell 命令

srcfiles := $(shell echo src/{00..99}.txt)

(2)wildcard 函数

wildcard 函数用来在 Makefile 中,替换 Bash 的通配符。

srcfiles := $(wildcard src/*.txt)

标签:foo,命令,make,makefile,echo,source,文件格式,txt
来源: https://blog.csdn.net/m0_65145426/article/details/122003179

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有