Makefile 与不同目录中的源文件 [英] Makefiles with source files in different directories

查看:58
本文介绍了Makefile 与不同目录中的源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,目录结构是这样的:

I have a project where the directory structure is like this:

                         $projectroot
                              |
              +---------------+----------------+
              |               |                |
            part1/          part2/           part3/
              |               |                |
       +------+-----+     +---+----+       +---+-----+
       |      |     |     |        |       |         |
     data/   src/  inc/  src/     inc/   src/       inc/

我应该如何编写一个可以在部分/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]

推荐答案

传统方式是在每个子目录(part1part2 等)允许您独立构建它们.此外,在项目的根目录中有一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆