c++ - 生成具有完整路径的 gcc 依赖项

标签 c++ makefile g++

我有一个看起来像这样的简单项目

.
├── build
│   ├── file1.o
│   └── one
│       ├── file1.o
│       └── file2.o
├── .depend
├── Makefile
└── src
    ├── file1.cpp
    └── one
        ├── file1.cpp
        └── file2.cpp

Makefile 是这样的:

# Get all of the source files
SRC = $(shell find src/ -name "*.cpp")
# Get all of the object files
OBJ = $(subst src,build,$(SRC:.cpp=.o))

$(OBJ):
    @mkdir -p $(shell dirname $@)
    g++ -g -c $(subst build,src,$(subst .o,.cpp,$@)) -o $@

all: depend build

build: $(OBJ)
    gcc -o project $^

depend:
    g++ -MM $(SRC) > .depend
    sed -i 's/.\/src/.\/build\//g' .depend

sinclude .depend

我正在尝试通过运行 g++ -MM src/file1.cpp src/one/file1.cpp src/one/file2.cpp > .depend 生成 makefile 依赖项,它生成以下内容指令:

file1.o: src/file1.cpp <other headers>
file1.o: src/one/file1.cpp <other headers>
file2.o: src/one/file2.cpp <other headers>

问题在于 build/file1.ofile1.o 不匹配,因此更改 src/file1.cpp 或它所依赖的任何 header 都不会导致目标文件被重建。起初我认为这可能是在生成 .depend 文件之前运行 sinclude .depend 的问题,但即使我运行 make depend 后跟构建。从我读过的所有内容来看,没有任何 g++ 参数或选项可以保留名称的路径。

是否可以通过这种方式生成依赖文件,或者这是一种根本不正确的构建项目的方法?

我查看了这个问题被标记为可能重复的问题的答案,但似乎问题是询问如何为项目创建完整的 makefile,而我的问题不是创建一个Makefile,而是 gcc -MM 依赖生成的问题。那个问题的答案没有解决我的问题。

最佳答案

关于:

# Get all of the source files
SRC = $(shell find src/ -name "*.cpp")
# Get all of the object files
OBJ = $(patsubst src/%.cpp,build/%.o,$(SRC))

.PHONY: all

all: project

project: $(OBJ)
    gcc -o $@ $^

$(OBJ): build/%.o: src/%.cpp
    @mkdir -p $(dir $@)
    g++ -g -c $< -o $@

.depend: $(SRC)
    g++ -MM $^ > $@ && \
    sed -Ei 's#^(.*\.o: *)src/(.*/)?(.*\.cpp)#build/\2\1src/\2\3#' $@

include .depend

依赖计算

sed 命令替换任何:

file.o: src/file.cpp ...

通过:

build/file.o: src/file.cpp ...

和任何:

file.o: src/X/Y/Z/file.cpp ...

通过:

build/X/Y/Z/file.o: src/X/Y/Z/file.cpp ...

目标直接是 .depend 并且它具有所有源文件作为依赖项,因此如果丢失或比任何源文件旧,它会自动重建。无需使用 depend 虚假目标或将其添加为 all 的先决条件(make 会自动尝试重建包含在 include 中的文件,如果需要的话)。

注意事项

我添加了一些 GNU make 功能(patsubst、静态模式规则、自动变量的系统使用...)如果您使用其他 make,请重新处理不受支持的功能。

关于c++ - 生成具有完整路径的 gcc 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47783125/

相关文章:

c++ - ignite C++ 客户端是否支持发布订阅

makefile - 多个目标名称具有相同的结果?

docker - make 命令在 Windows WSL Ubuntu 中不起作用

C++11 for_each 和 lambdas 优化

c++ - MFC 应用程序中的堆栈溢出 C00000FD

c++ - 求 `hires_time_in_seconds()`的C++实现

c++ - CppCMS URL 调度和映射失败。代码不完整?

shell - pushd 在 makefile 中不起作用

c++ - 我可以从 C++ 库中包含/导入单个函数吗

gcc - 如何查看 -march=native 将激活哪些标志?