在不同目录中具有源文件的Makefile [英] Makefiles with source files in different directories
问题描述
我有一个目录结构如下的项目:
I have a project where the directory structure is like this:
$projectroot
|
+---------------+----------------+
| | |
part1/ part2/ part3/
| | |
+------+-----+ +---+----+ +---+-----+
| | | | | | |
data/ src/ inc/ src/ inc/ src/ inc/
我应该如何编写一个可以在part/src(或实际位置)中生成的,可以部分地在c/c ++源文件上完成/链接的makefile?/src?
How should I write a makefile that would be in part/src (or wherever really) that could comple/link on the c/c++ source files in part?/src ?
我可以做类似的事情吗 -I $ projectroot/part1/src -I $ projectroot/part1/inc -I $ projectroot/part2/src ...
Can I do something like -I$projectroot/part1/src -I$projectroot/part1/inc -I$projectroot/part2/src ...
如果这行得通,有没有更简单的方法来做到这一点.我见过在每个相应部分中都有一个makefile的项目吗?文件夹. [在这篇文章中,我在bash语法中使用了问号]
If that would work, is there an easier way to do it. I've seen projects where there is a makefile in each of the corresponding part? folders. [in this post I used the question mark like in bash syntax]
推荐答案
传统方法是在每个子目录(part1
,part2
等)中都有一个Makefile
,允许您独立构建它们.此外,在项目的根目录中有一个Makefile
,它可以构建所有内容. 根" Makefile
看起来类似于以下内容:
The traditional way is to have a Makefile
in each of the subdirectories (part1
, part2
, etc.) allowing you to build them independently. Further, have a Makefile
in the root directory of the project which builds everything. The "root" Makefile
would look something like the following:
all:
+$(MAKE) -C part1
+$(MAKE) -C part2
+$(MAKE) -C part3
由于make目标中的每一行都在其自己的shell中运行,因此无需担心遍历目录树或其他目录.
Since each line in a make target is run in its own shell, there is no need to worry about traversing back up the directory tree or to other directories.
我建议您看一下 GNU make手册第5.7节;这非常有帮助.
I suggest taking a look at the GNU make manual section 5.7; it is very helpful.
这篇关于在不同目录中具有源文件的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!