0

我有以下生成文件:

all: a.out b.out
.PHONY: gen_hdr1
gen_hdr1:
    #call script 1 that generates x.h
    rm a.o #try to force rebuild of a.cpp

.PHONY: gen_hdr2
gen_hdr2:
    #call script 2 that generates x.h
    rm a.o #try to force rebuild of a.cpp

b.out: gen_hdr2 a.o
    g++ -o b.out a.o

a.out: gen_hdr1 a.o
    g++ -o a.out a.o
*.o : *.cpp
    g++ -c $< -o $@

a.cpp 包含 xh

我想做的事:

  1. 删除 ao 如果存在
  2. 为 App A 生成 xh
  3. 编译.cpp
  4. 构建应用程序 A
  5. 删除 ao 如果存在
  6. 为 App B 生成 xh
  7. 再次编译 a.cpp
  8. 构建应用程序 B

运行makefile的输出是:

#call script 1 that generates x.h
rm -f a.o #try to force rebuild of a.cpp
g++    -c -o a.o a.cpp
g++ -o a.out a.o
#call script 2 that generates x.h
rm -f a.o #try to force rebuild of a.cpp
g++ -o b.out a.o
g++: a.o: No such file or directory
g++: no input files
make: *** [b.out] Error 1

基本上,在构建 App B 时找不到 ao。如何强制 make 系统重建它?

4

1 回答 1

2

解决这类问题的好方法是使用单独的构建对象文件夹,每个目标多一个子文件夹。

因此,您将拥有类似的东西:

build/first/a.o: src/a.cpp gen/a.h
    # Do you stuff in here
gen/a.h:
    # Generate you .h file if needed

build/second/a.o: src/a.cpp gen/a.h
    # Same thing

使用此解决方案,您将在 build 文件夹中拥有所有构建对象,因此 clean 命令更简单一些:

clean:
    rm -rf build/*
    rm -rf gen/*
    rm -rf bin/*

您应该确保的唯一一件事是该目录在构建之前存在,但这不是一项艰巨的工作:)

如果你必须生成两个版本的啊,你可以使用相同的设计(gen/first & gen/second 文件夹)。

希望它有帮助,如果我错过了什么,请告诉我

于 2012-04-18T07:50:51.580 回答