且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Makefile改进,依赖生成不起作用

更新时间:2023-02-07 13:18:51

这里。

分配 变量

Assign simply expanded variables where possible:

GNU Make手册


递归扩展变量的另一个缺点是,定义中引用的任何函数每次变量被扩展时执行。这使得 make 运行更慢;更糟的是,它会导致通配符 shell 函数给出不可预测的结果,因为您无法轻松控制调用时间,即使多少次。

From GNU Make manual:






使用替换参考 patsubst 函数将源代码转换为对象:

Another disadvantage [of recursively expanded variables] is that any functions referenced in the definition will be executed every time the variable is expanded. This makes make run slower; worse, it causes the wildcard and shell functions to give unpredictable results because you cannot easily control when they are called, or even how many times.




Use substitution references or patsubst function to convert sources into objects:






在编译模式规则中指定适当的先决条件。

OBJS := $(SRCS:$(SRC_DIR)/%$(SRC_EXT)=$(OBJ_DIR)/%$(OBJ_EXT))




Specify proper prerequisites in compilation pattern rule. This is mandatory to get Make keeping your object files up to date and updating them on source changes.






编译源代码并为其生成依赖文件同时。使用 -MMD -MP 标志来完成工作(只需将它们附加到 CXXFLAGS )。


Compile sources and generate dependency files for them at the same time. Use -MMD -MP flags to get things work (just append them to CXXFLAGS).

CXXFLAGS += -MMD -MP

-include $(OBJS:$(OBJ_EXT)=.d)

GCC手册


-MD

-MD 等效于 -M -M ,除非 -E 不是隐含的。驱动程序根据是否给出-o选项来确定文件。如果是,驱动程序使用它的参数,但后缀为 .d ,否则它接受输入文件的名称,删除任何目录组件和后缀,并应用 .d 后缀。

-MD is equivalent to -M -MF file, except that -E is not implied. The driver determines file based on whether an -o option is given. If it is, the driver uses its argument but with a suffix of .d, otherwise it takes the name of the input file, removes any directory components and suffix, and applies a .d suffix.

-MMD

-MD ,只提及用户头文件,而不是系统头文件。

Like -MD except mention only user header files, not system header files.

-MP

此选项指示CPP为每个依赖项添加一个假目标,而不是主文件,导致每个依赖什么。这些伪规则处理错误 make 给出如果你删除头文件而不更新 Makefile 匹配。

This option instructs CPP to add a phony target for each dependency other than the main file, causing each to depend on nothing. These dummy rules work around errors make gives if you remove header files without updating the Makefile to match.

也请考虑学习这个文章(他是GNU Make的维护者)。它给出了不同自动生成方法的相当好的概述。

Also consider studying this article of Paul Smith (he is a maintainer of GNU Make). It gives a rather good overview of different autodep-generation approaches.