【问题标题】:What files are actually included when compiling编译时实际包含哪些文件
【发布时间】:2010-10-10 15:24:48
【问题描述】:

我有一个非常大的代码,其中很多是遗留代码。 我想知道所有这些文件中的哪些参与了编译。 代码是用 GNU 编译器编写的,主要是用 C/C++ 编写的,但也有一些是在其他程序中编写的。 任何建议将不胜感激。

谢谢,

莫舍。

我正在使用混合脚本/makefile 在 linux 下编译。我想以某种方式“包装”这个构建,该工具将提供构建中使用的所有源文件的输出,最好使用绝对路径名。

你说什么?

【问题讨论】:

  • 使用什么构建系统?这可能有助于弄清楚正在编译的内容。例如,如果您有一个 makefile 可能是一个开始的地方。

标签: c++ c gcc g++


【解决方案1】:

如果您想显示包含的标头,那么是否支持以及如何执行取决于编译器。

例如,

C:\test> (g++ --help --verbose 2>&1) | find "header"
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  --sysroot=<directory>    Use <directory> as the root directory for headers
  -H                          Print the name of header files as they are used
  -MG                         Treat missing header files as generated files
  -MM                         Like -M but ignore system header files
  -MMD                        Like -MD but ignore system header files
  -MP                         Generate phony targets for all headers
  -Wsystem-headers            Do not suppress warnings from system headers
  -print-objc-runtime-info    Generate C header of platform-specific features
  -ftree-ch                   Enable loop header copying on trees

C:\test> (cl /? 2>&1) | find "include"
/FI<file> name forced include file      /U<name> remove predefined macro
/u remove all predefined macros         /I<dir> add to include search path
/nologo suppress copyright message      /showIncludes show include file names

C:\test> _

在上面你可以分别看到g++和Visual C++的相关选项。

干杯,

--阿尔夫

【讨论】:

    【解决方案2】:

    对于给定的编译单元,例如foo.cpp,将标志 -E -g3 添加到 g++ 的调用中。 这为您提供了预处理的代码。在那里您可以查看包含哪些内容。

    【讨论】:

      【解决方案3】:

      我想到了两个选项。

      1. 解析编译日志

        运行构建,保存日志,然后在日志中搜索。

      2. 查找在编译期间打开的文件。

        一种方法可能是使用像strace 这样的系统跟踪工具或像ltrace 这样的库跟踪工具,然后注意文件打开调用。

        另见How can I detect file accesses in Linux?

      【讨论】:

        【解决方案4】:
        1. 如何构建应用程序? IE。你在终端输入什么来构建它?
        2. 根据您对 (1) 的回答,找到用于构建的相关程序(即makescons 等)
        3. 现在找到该构建程序的输入文件,例如MakefileSConstruct 等。
        4. 查看此构建文件和它使用的其他构建文件以确定哪些源文件进入了构建

        【讨论】:

        • 您好 Eli,您的解决方案肯定能奏效。然而,由于大部分代码是遗留代码(包括许多 make 文件等),我更愿意找到某种“包装”程序,它可能能够为我过滤数据。谢谢摩西。
        【解决方案5】:

        这是一种使用make 查找所有包含文件的技术。 它是非侵入性的,因此您无需对文件进行任何更改,甚至无需实际编译。 Make 将为您完成所有工作。

        make -d
        

        将运行 make 并发出大量描述 make 过程的内部处理的行。最重要的是依赖关系的考虑。

        解析输出很容易找到依赖项和所有其他文件。

        这是一个 Linux 命令行,它获取包含包含文件的目录的排序列表:

        make -d | awk '/Prerequisite/ { if(match($2,".(.*)(/)(.*\\.h)",m)) { c[m[1]]++ ; } } END {for(d in c) print "\"" d "\",";} ' | sort
        

        在这种情况下,引用目录并在末尾添加逗号,因此输出已准备好包含在 Visual Studio Code (vscode) 配置文件 c_cpp_properties.json

        简单的变体可以生成包含依赖项的宏列表,如下所示:

        make -d | awk '/Prerequisite/ { if(match($2,".(.*\\.h)",m)) { c[m[1]]++ ; } } END {for(d in c) print  d ;} ' | sort
        

        这也应该适用于目标(例如make All

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-11
          • 1970-01-01
          • 2013-09-20
          • 1970-01-01
          相关资源
          最近更新 更多