【问题标题】:List of all C or C++ header files that were used for compiling a project用于编译项目的所有 C 或 C++ 头文件的列表
【发布时间】:2020-06-19 11:47:11
【问题描述】:

我想知道编译整个 C/C++ 项目的所有代码文件的路径,其中包含多个生成的二进制文件。我知道https://github.com/rizsotto/Bear et al,您可以将其包含在您的“make”调用中,并告诉您 gcc/g++ 调用,但它们当然只向我显示已编译的 C/C++ 文件 - 我怎样才能找出哪个编译需要这些头文件并最终出现在其中一个生成的二进制文件中?

【问题讨论】:

  • 使用覆盖输出编译,这应该为您提供必要的信息。 (最简单的方法可能是实际获得覆盖率输出......)
  • 只能通过预处理器运行编译,“grep”用于“include”
  • @chux-ReinstateMonica 预处理后的输入中不会有任何#includes,尽管可能存在具有不同文件名的#line 指令。
  • @1201ProgramAlarm 是的,这是通常的情况。我记得使用这种方法是因为 include file 被保留为评论。

标签: c++ c gcc g++


【解决方案1】:

GCC 有运行时选项-M。来自 GCC 手册页:

   -M  Instead of outputting the result of preprocessing, output a rule
       suitable for make describing the dependencies of the main source
       file.  The preprocessor outputs one make rule containing the object
       file name for that source file, a colon, and the names of all the
       included files, including those coming from -include or -imacros
       command-line options.

       Unless specified explicitly (with -MT or -MQ), the object file name
       consists of the name of the source file with any suffix replaced
       with object file suffix and with any leading directory parts
       removed.  If there are many included files then the rule is split
       into several lines using \-newline.  The rule has no commands.

       This option does not suppress the preprocessor's debug output, such
       as -dM.  To avoid mixing such debug output with the dependency
       rules you should explicitly specify the dependency output file with
       -MF, or use an environment variable like DEPENDENCIES_OUTPUT.
       Debug output is still sent to the regular output stream as normal.

       Passing -M to the driver implies -E, and suppresses warnings with
       an implicit -w.

Clang 支持相同的选项

【讨论】:

    【解决方案2】:

    GCC 和 Clang 都支持 -H 选项(以及 -M option 及其亲属)。

    -H

    打印使用的每个头文件的名称,以及其他正常活动。每个名称都缩进以显示它在“#include”堆栈中的深度。预编译的头文件也会被打印出来,即使被发现是无效的;一个无效的预编译头文件用“...x”打印,一个有效的用“...!”打印。

    -H 输出报告标准错误,而不是标准输出。

    -M 选项相比,-H 选项提供了更多不同的信息。它报告嵌套级别(一个或多个前导点),并在每次包含标题时显示 - 即使内容因标题保护宏而被忽略:

    #ifndef HEADER_H_INCLUDED
    #define HEADER_H_INCLUDED
    
    …content of header…
    
    #endif /* HEADER_H_INCLUDED */
    

    这很有用;具有格式化的make 依赖行(由-M 生成)也很有用。


    示例输出(从我的一个程序编译一个源文件——make 当然生成了编译器命令行):

    $ gcc -H -g -O3 -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -DSYSTEM=MACOS_X -DSTR_SYSTEM='"(macOS Mojave 10.14.6 - Darwin 18.7.0)"' -I/Users/jonathanleffler/inc -DDEBUG -DHASH_STATISTICS -c main.c
    . make.h
    .. /Users/jonathanleffler/inc/posixver.h
    .. config.h
    .. /usr/include/assert.h
    ... /usr/include/sys/cdefs.h
    .... /usr/include/sys/_symbol_aliasing.h
    .... /usr/include/sys/_posix_availability.h
    .. /usr/include/ctype.h
    ... /usr/include/_ctype.h
    .... /usr/include/runetype.h
    ..... /usr/include/_types.h
    ...... /usr/include/sys/_types.h
    ....... /usr/include/machine/_types.h
    ........ /usr/include/i386/_types.h
    ....... /usr/include/sys/_pthread/_pthread_types.h
    .. /usr/include/errno.h
    ... /usr/include/sys/errno.h
    .. /usr/include/inttypes.h
    ... /usr/include/Availability.h
    .... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/AvailabilityInternal.h
    ... /usr/include/sys/_types/_wchar_t.h
    ... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include/stdint.h
    .... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/stdint.h
    ..... /usr/include/sys/_types/_int8_t.h
    ..... /usr/include/sys/_types/_int16_t.h
    ..... /usr/include/sys/_types/_int32_t.h
    ..... /usr/include/sys/_types/_int64_t.h
    ..... /usr/include/_types/_uint8_t.h
    ..... /usr/include/_types/_uint16_t.h
    ..... /usr/include/_types/_uint32_t.h
    ..... /usr/include/_types/_uint64_t.h
    ..... /usr/include/sys/_types/_intptr_t.h
    ...... /usr/include/machine/types.h
    ....... /usr/include/i386/types.h
    ........ /usr/include/sys/_types/_u_int8_t.h
    ........ /usr/include/sys/_types/_u_int16_t.h
    ........ /usr/include/sys/_types/_u_int32_t.h
    ........ /usr/include/sys/_types/_u_int64_t.h
    ........ /usr/include/sys/_types/_intptr_t.h
    ........ /usr/include/sys/_types/_uintptr_t.h
    ..... /usr/include/_types/_intmax_t.h
    ..... /usr/include/_types/_uintmax_t.h
    .. /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/limits.h
    ... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/syslimits.h
    .... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/limits.h
    ..... /usr/include/limits.h
    ...... /usr/include/machine/limits.h
    ....... /usr/include/i386/limits.h
    ........ /usr/include/i386/_limits.h
    ...... /usr/include/sys/syslimits.h
    .. /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include/stdbool.h
    .. /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/stdio.h
    ... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include/stdarg.h
    ... /usr/include/_stdio.h
    .... /usr/include/sys/_types/_va_list.h
    .... /usr/include/sys/_types/_size_t.h
    .... /usr/include/sys/_types/_null.h
    .... /usr/include/sys/stdio.h
    ... /usr/include/sys/_types/_off_t.h
    ... /usr/include/sys/_types/_ssize_t.h
    ... /usr/include/secure/_stdio.h
    .... /usr/include/secure/_common.h
    .. /usr/include/stdlib.h
    ... /usr/include/sys/wait.h
    .... /usr/include/sys/_types/_pid_t.h
    .... /usr/include/sys/_types/_id_t.h
    .... /usr/include/sys/signal.h
    ..... /usr/include/sys/appleapiopts.h
    ..... /usr/include/machine/signal.h
    ...... /usr/include/i386/signal.h
    ..... /usr/include/machine/_mcontext.h
    ...... /usr/include/i386/_mcontext.h
    ....... /usr/include/mach/machine/_structs.h
    ........ /usr/include/mach/i386/_structs.h
    ..... /usr/include/sys/_pthread/_pthread_attr_t.h
    ..... /usr/include/sys/_types/_sigaltstack.h
    ..... /usr/include/sys/_types/_ucontext.h
    ...... /usr/include/machine/_mcontext.h
    ..... /usr/include/sys/_types/_sigset_t.h
    ..... /usr/include/sys/_types/_uid_t.h
    .... /usr/include/sys/resource.h
    ..... /usr/include/sys/_types/_timeval.h
    ... /usr/include/sys/_types/_wchar_t.h
    ... /usr/include/malloc/_malloc.h
    .. /usr/include/string.h
    ... /usr/include/secure/_string.h
    .. /usr/include/sys/stat.h
    ... /usr/include/sys/_types/_timespec.h
    ... /usr/include/sys/_types/_blkcnt_t.h
    ... /usr/include/sys/_types/_blksize_t.h
    ... /usr/include/sys/_types/_dev_t.h
    ... /usr/include/sys/_types/_ino_t.h
    ... /usr/include/sys/_types/_mode_t.h
    ... /usr/include/sys/_types/_nlink_t.h
    ... /usr/include/sys/_types/_gid_t.h
    ... /usr/include/sys/_types/_time_t.h
    ... /usr/include/sys/_types/_s_ifmt.h
    .. /usr/include/unistd.h
    ... /usr/include/sys/unistd.h
    .... /usr/include/sys/_types/_posix_vdisable.h
    .... /usr/include/sys/_types/_seek_set.h
    ... /usr/include/sys/_types/_useconds_t.h
    .. /Users/jonathanleffler/inc/debug.h
    ... /Users/jonathanleffler/inc/kludge.h
    .. /Users/jonathanleffler/inc/emalloc.h
    .. list.h
    .. /Users/jonathanleffler/inc/sastrings.h
    .. /Users/jonathanleffler/inc/stderr.h
    ... /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include/stdarg.h
    . /Users/jonathanleffler/inc/getopt.h
    Multiple include guards may be useful for:
    /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/syslimits.h
    /usr/include/assert.h
    /usr/include/errno.h
    /usr/include/machine/limits.h
    /usr/include/secure/_stdio.h
    /usr/include/secure/_string.h
    /usr/include/sys/_posix_availability.h
    /usr/include/sys/_symbol_aliasing.h
    /usr/include/sys/_types/_seek_set.h
    $
    

    列出 110 个标题;只有五个重复:

    2 /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include-fixed/limits.h
    2 /opt/gcc/v9.2.0/lib/gcc/x86_64-apple-darwin18.7.0/9.2.0/include/stdarg.h
    2 /usr/include/machine/_mcontext.h
    2 /usr/include/sys/_types/_intptr_t.h
    2 /usr/include/sys/_types/_wchar_t.h
    

    我从事的其他项目有更多重复。取一个源文件,或多或少是随机的(我知道这是一个大文件;我不认为这是最糟糕的),有一个包含 4 次或更多次的标题列表 - 有很多包含 2 次和 3 次。此文件的 -H 的标题行总数为 592。项目特定的目录和文件名已更改以保护无辜者 - 文件名已使用 realpath(2) 进行处理以处理 @987654323 的特殊使用@ 源代码和包含的标头中的样式名称,-H 扩展为以下名称:

    ../incl/../subdir1/../subdir1/../subdir2/../subdir2/header27.h
    

    计数:

      4 /usr/include/errno.h
      4 /usr/include/time.h
      4 /opt/project/incl/header1.h
      4 /opt/project/incl/header2.h
      4 /opt/project/subdir/header3.h
      4 /opt/project/subdir/header4.h
      4 /opt/project/subdir/header5.h
      5 /opt/project/incl/header6.h
      5 /opt/project/subdir/header7.h
      6 /opt/project/subdir/header8.h
      6 /opt/project/subdir/header9.h
      6 /work5/gcc/v9.2.0/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include-fixed/limits.h
      7 /opt/project/subdir/header10.h
     14 /usr/include/bits/wordsize.h
     14 /opt/project/subdir/header11.h
     22 /work5/gcc/v9.2.0/lib/gcc/x86_64-pc-linux-gnu/9.2.0/include/stddef.h
    

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2018-02-02
      • 2013-01-04
      相关资源
      最近更新 更多