gpt4 book ai didi

c++ - 管理生成文件中的依赖关系复杂性

转载 作者:行者123 更新时间:2023-11-30 04:24:35 24 4
gpt4 key购买 nike

我正在开发我的第一个开源 C++ 项目:https://github.com/jehugaleahsa/spider-cpp .

我正在管理自己的 Makefile,并且我有关于如何管理依赖项的“最佳实践”问题。现在,我使每个 .o 文件都依赖于 .cpp 文件中包含的每个头文件。所以:

code.o: code.cpp code.hpp dep1.hpp de2.hpp
g++ -c code.cpp

首先,我很确定 Make 支持创建目标文件的简写。如果有人能举出这方面的例子,我将不胜感激。

接下来,有没有办法避免将每个包含的 header 都列为依赖项?我想确保如果我更改依赖项,更改仍然兼容。列出包含的 header 很乏味,而且很容易搞砸。

最佳答案

运算符(operator):

First of all, I am pretty sure Make supports a shorthand for creating object files. If someone would show an example of this, I'd appreciate it.

来自 here :

OBJS := foo.o bar.o

#Your program should have the objects as dependencies, and link them
proggie: $(OBJS)
gcc $(OBJS) -o proggie


# compile
%.o: %.c
gcc -c $(CFLAGS) $*.c -o $*.o

运算符(operator):

Next, is there a way to avoid listing every included header as a dependency

降低on the same page ,请参阅以下行:

# pull in dependency info for *existing* .o files
-include $(OBJS:.o=.d)


# compile and generate dependency info
%.o: %.c
gcc -c $(CFLAGS) $*.c -o $*.o
gcc -MM $(CFLAGS) $*.c > $*.d

基本上,它所做的是使用 gcc 的 -MM 选项来获取头文件列表,现在我们可以依赖它们了。因此,我们将包含此类头文件列表的文件输出到 .d 文件,然后下一次,我们将文件列表添加为依赖项,这就是 -include 命令所做的。如果依赖项 .d 文件尚不存在,则“-”会避免错误。

请注意,您应该修改以上内容以说明 .cpp 文件

关于c++ - 管理生成文件中的依赖关系复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com