如何创建非传递依赖列表 (GCC)

发布于 2024-10-20 16:10:25 字数 528 浏览 2 评论 0原文

有没有一种方法可以生成 C(++) 源文件的依赖项,类似于使用 GCC 的 -MM 选项,仅包含该文件的直接依赖项,即仅包含该源文件直接包含的文件?

关于我为什么要寻找此功能的更多背景信息 - 也许对我的问题有一个完全不同的解决方案:我有一个通用的 makefile,可以自动检测依赖项,可以满足我的需求,但速度很慢。基本结构如下:

  • 使用 gcc -MM 检索 main.cpp 的完整依赖项 存在
  • 相应 *.cpp 的所有 *.h 依赖项均更改为 *.o 依赖项
  • 更改后的依赖项包含在 makefile 中
  • 所有 *.构建 o 目标,使用 gcc -MM 检索依赖项并包含
  • 所有 *.o 目标链接以创建可执行文件

到目前为止,这个 makefile 工作正常,但 - 正如前面所说 - 它很慢。我分析了一个项目的执行路径,并手动包含了所有生成的依赖项,以尝试优化其速度。结果是,通过删除所有传递依赖项,makefile 保留了其功能,但速度更快(也反映在 make -d 的调试输出的行数中)。

Is there a way to generate dependencies of a C(++) source file similar to using the -MM option of GCC that only include the direct dependencies of said file, i.e. only the files directly included by this source file?

More context on why I'm looking for this functionality - maybe there is a completely different solution to my problem: I have a generic makefile with auto-detection of dependencies that suffices my needs but is slow. The basic structure is as follows:

  • Full dependencies of main.cpp are retrieved with gcc -MM
  • All *.h dependencies for which a corresonding *.cpp exists are changed to *.o dependencies
  • the altered dependencies are included in the makefile
  • All *.o targets are built, dependencies are retrieved with gcc -MM and included
  • All *.o targets are linked to create the executable

So far, this makefile has worked fine but -as said before- it is slow. I analyzed its execution path for one project and included all the generated dependencies by hand to try and optimize its speed. The result was by removing all transitive dependencies, the makefile retained its functionality but got much faster (also reflected in the number of lines of the debug output of make -d).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-10-27 16:10:25

首先,您使用的方法有点令人困惑。编译一个 .cpp 文件时使用的所有 .h 文件必须保留在其依赖项中,并且 *.o 文件的自动收集不应那么慢。我建议使用经典的 -MM 并构建要手动编译的 cpp 文件列表。 automake 就是这样做的,如果有一种真正可靠的方法来自动计算出编译单元列表,这些人就会找到它:-)。

不过,gcc 的 -H 选项可以帮助您。它将所有使用的文件的名称打印到 stderr,扩展名为 .包含级别的前缀。所以,

cpp -I 
lt; >/dev/null | sed -n -e 's/^\. //p'

应该做到这一点。警告:如果头文件首先包含在层次结构的较深处,然后再包含在主文件中,则找不到该头文件。但是,如果您保持 .h 和 .cpp 文件一一对应,您的依赖系统应该能够处理该问题。

First of all, the method you are using is slightly confusing. All .h files used in the compilation of one .cpp file must be kept in its dependencies, and the automatic collection of *.o files shouldn't be that slow. I'd advise to go with classic -MM and to build the list of cpp files that are to be compiled by hand. automake does it that way, and if there was a really reliable way of figuring out the list of compilation units automatically, these guys would have found it :-).

Nevertheless, the -H option of the gcc helps you. It prints the names of all used files to stderr, with . prefixed for the level of inclusion. So,

cpp -I 
lt; >/dev/null | sed -n -e 's/^\. //p'

should do the trick. Caveat: If a header file is included deeper in the hierarchy first and then later in the main file, it is not found. However, your dependency system should be able to handle that if you keep the 1-to-1 of .h and .cpp files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文