2012-04-18 38 views
0

我有以下生成文件:力的makefile构建来源两次

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 [email protected] 

a.cpp includex XH

我想要做什么:

  1. 是否存在
  2. 删除AO为应用A生成xh
  3. 编译a.cpp
  4. 构建应用甲
  5. 如果存在
  6. 生成XH为App乙
  7. 编译a.cpp再次
  8. 构建应用B

运行生成文件的输出被除去AO:

#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 

基本上,应用程序B构建时找不到ao。 如何强制make系统重建它?

+0

你有没有尝试过让'a.o'目标虚假?即'.PHONY:a.o' – 2012-04-18 07:36:16

+0

是的,我将***。o:* .cpp **的一般规则替换为**。PHONY:a.o a.o:a.cpp **;这是你的意思? – 2012-04-18 07:57:52

回答

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: 
    rm -rf build/* 
    rm -rf gen/* 
    rm -rf bin/* 

你应该确保的唯一的事情是该目录存在之前建立,但这不是一个虽然工作要做:)

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

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

+0

谢谢,很好的解决方案! – 2012-04-18 10:41:22