Linux 的make及makefile文件格式(1),一次违反常规的Linux运维大厂面试经历

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.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)

(3)subst 函数

subst 函数用来文本替换,格式如下。

$(subst from,to,text)

下面的例子将字符串"feet on the street"替换成"fEEt on the strEEt"。

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

下面是一个稍微复杂的例子。

comma:= ,

empty:=

# space变量用两个空变量作为标识符,当中是一个空格

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)
img

为了做好运维面试路上的助攻手,特整理了上百道 【运维技术栈面试题集锦】 ,让你面试不慌心不跳,高薪offer怀里抱!

这次整理的面试题,小到shell、MySQL,大到K8s等云原生技术栈,不仅适合运维新人入行面试需要,还适用于想提升进阶跳槽加薪的运维朋友。

本份面试集锦涵盖了

  • 174 道运维工程师面试题
  • 128道k8s面试题
  • 108道shell脚本面试题
  • 200道Linux面试题
  • 51道docker面试题
  • 35道Jenkis面试题
  • 78道MongoDB面试题
  • 17道ansible面试题
  • 60道dubbo面试题
  • 53道kafka面试
  • 18道mysql面试题
  • 40道nginx面试题
  • 77道redis面试题
  • 28道zookeeper

总计 1000+ 道面试题, 内容 又全含金量又高

  • 174道运维工程师面试题

1、什么是运维?

2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?

3、现在给你三百台服务器,你怎么对他们进行管理?

4、简述raid0 raid1raid5二种工作模式的工作原理及特点

5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

7、Tomcat和Resin有什么区别,工作中你怎么选择?

8、什么是中间件?什么是jdk?

9、讲述一下Tomcat8005、8009、8080三个端口的含义?

10、什么叫CDN?

11、什么叫网站灰度发布?

12、简述DNS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

工作模式的工作原理及特点

5、LVS、Nginx、HAproxy有什么区别?工作中你怎么选择?

6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?

7、Tomcat和Resin有什么区别,工作中你怎么选择?

8、什么是中间件?什么是jdk?

9、讲述一下Tomcat8005、8009、8080三个端口的含义?

10、什么叫CDN?

11、什么叫网站灰度发布?

12、简述DNS进行域名解析的过程?

13、RabbitMQ是什么东西?

14、讲一下Keepalived的工作原理?

15、讲述一下LVS三种模式的工作过程?

16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?

17、如何重置mysql root密码?

一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!

AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值