makefile写法
# tab和空格缩进
https://blog.csdn.net/QQ839787886/article/details/108573298 (opens new window)
makefile实际上是在一个文件中用两种完全不同的“语言”编写的。
recipe(运行编译器,echo等的命令)是用shell脚本语法编写的。
不在recipe中的其余makefile是用makefile语法编写的。
为了使make能够区分recipe和不是recipe的东西,它使用了TAB字符。因此,
以TAB开头的行被假定为recipe的一部分,因此它们是shell脚本并传递给shell进行解析。
以空格开头的行不能成为recipe的一部分,它们必须是makefile语法,用空格。
ifneq是makefile语法:它不能成为recipe的一部分,并且以空格开头。
结论:
1、在写makefile语法,非recipe的时候,缩进应该使用空格。
2、在写shell语法,recipe时,缩进使用TAB。因为实际上我们写的是希望shell执行的语句,所以使用的是shell syntax。而make识别recipe的方式就是。
# 编译所有cpp
https://blog.csdn.net/zong596568821xp/article/details/81134406
linux,假设有若干cpp和h文件在src文件夹下,下面的makefile将生成.o文件在build文件夹下,并可执行文件生成app在当前目录。
target = app
src = $(wildcard ./src/*.cpp)
obj = $(patsubst ./src/%.cpp, ./build/%.o, $(src))
$(target): $(obj)
g++ $(obj) -o $(target) -std=c++11 -pthread
./build/%.o: ./src/%.cpp
g++ -c $< -o $@ -std=c++11 -pthread
clean:
rm $(obj) $(target)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# ifeq用法
ifndef mode
mode = release
endif
ifeq ($(mode), debug)
CFLAGS += -g -O0
else ifeq ($(mode), release)
CFLAGS += -g -O0
else
$(error "error: unknown build mode!!!")
endif
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
注意 $(error "xxx") 不能用tab缩进,并且该语句会使make命令终止。
编辑 (opens new window)
上次更新: 2023/01/28, 16:16:57