gpt4 book ai didi

c++ - Makefile,源码在多个目录,依赖问题

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

我正在试验 makefile...主要是当源文件位于许多目录中时

我有以下情况...

project/src/包含目录 A、B、Main

目录A包含A.h和A.cpp; B包含B.cpp和B.h;和测试包含 test.cpp

A.cpp 包括 A.h; B.cpp包含B.h,Main.cpp包含A.h、B.h

项目/lib 包含 libA.a libB.a

A 和 B 的 Makefile 很好...没问题...我正在从对象创建库和然后将它们复制到 lib 目录中

例如。目录 A 的 makefile,B 的类似文件


all:test
test : A.cpp A.hh
g++ -c A.cpp -o A
ar cru libA.a A.o
cp libA.a pathto/project/lib

I have makefile for Main directory as


all: test
test: test.o
g++ -I.. -L../../lib test.o -o test -lA -lB
test.o : test.cpp
g++ -c test.cpp -o test.o


<p>Everything works fine...only thing that I want to solve is that final executable 'test'
depends on objects from libA and libB, so when A.h or A.cpp or B.h or B.cpp changes, it should be made again
So, I now changed my makefile as
</p><pre><code>
test: test.o ../../libA.a ../../libB.a
g++ -I.. -L../../lib test.o -o test -lA -lB
</code></pre><p></p>

<p>Now, problem is how I can modify this so that it will make test again only when its
dependencies are newer than the 'test'.
There is no direct rule to make libA and libB, which Makefile requires and complains about;
since I am copying these libs from directory A and B into directory project/lib.</p>

<p>So, I guess one solution would be to call make in respective directory A and B when anything is new than 'test' but how do I exactly do that ? Or, any other better solution is appreciated.</p>

<p>Thanks a lot :)</p>

<p>EDIT</p>

<p>Here what I did and it solved the problem</p>

<p></p><pre>.PHONY : libA libB<p></p>

<p>../../libA.a : libA
libA :
cd pathtoDirA; $(MAKE)</p>

<p>../../libB.a : libB
libB :
cd pathtoDirB; $(MAKE)
</p></pre>

最佳答案

您确实需要添加一个知道如何生成 libA 和 libB 的规则,然后将来自测试的依赖项添加到该规则上。该规则可以在该目录中调用 make(递归 make),或者显式编码用于在您的 makefile 中构建库的规则。第一个更传统,也很容易理解。它几乎适用于您在现场遇到的所有情况,但也有一些 potential issues如果您有更复杂的build设置,就会出现这种情况(无论如何我可能会选择它,因为它更简单)。

关于c++ - Makefile,源码在多个目录,依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1161582/

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