如何看待包含文件的预处理后的实际顺序? [英] How to see the actual order of include files after preprocessing?

查看:224
本文介绍了如何看待包含文件的预处理后的实际顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.cpp文件,其中包含一些头文件。这些头文件也可以包括其它头文件。包含警卫是为了防止包含相同的文件两次。

I have one .cpp file that includes a few header files. These header files may include other header files as well. Include guards are in place to prevent including the same file twice.

知道每个文件只包括一次。有没有办法找出所有标头将被包括在内的最终顺序?

Knowing that each file is only included once. Is there a way to figure out the eventual order in which all headers will be included?

我试过 gcc -E 获得预处理器输出,但生成的代码似乎不可用于提取我想要的信息。

I tried gcc -E to get the preprocessor output, but the generated code doesn't seem usable for extracting the information that I want. Can anyone help?

我要求的原因是因为我需要包含我的标题

The reason why I'm asking is because I need to include my header files in a SWIG interface file in the correct order to avoid generation of weird SWIGTYPE_p_* wrappers.

感谢您的答案。使用 cpp -H 似乎非常有用。但是,我找不到办法 grep sed 这些结果为了得到简单的头文件列表在正确的顺序和没有重复。

Thanks for the answers. Using cpp -H seems very useful. However, I can't find way to grep or sed these results in order to get the simple list of header files in the correct order and without duplicates.

推荐答案

使用 cpp -H 。这将打印用于标准错误的标题。示例:

Use cpp -H. This prints the headers used to standard error. Example:

$ cpp -H -I../../include base64.cpp 2>&1 >/dev/null | head
. /usr/include/c++/4.4/cstring
.. /usr/include/c++/4.4/i486-linux-gnu/bits/c++config.h
... /usr/include/c++/4.4/i486-linux-gnu/bits/os_defines.h
.... /usr/include/features.h
..... /usr/include/bits/predefs.h
..... /usr/include/sys/cdefs.h
...... /usr/include/bits/wordsize.h
..... /usr/include/gnu/stubs.h
...... /usr/include/bits/wordsize.h
...... /usr/include/gnu/stubs-32.h

请参阅 GNU cpp手册

EDIT 脚本,将显示包含的顺序,假设所有头都包含警卫:

EDIT Here's a small Python script that will show the order of inclusion, assuming all headers have include guards:

import sys
seen = set()
for ln in sys.stdin:
    dots, header = ln.rstrip().split(' ', 1)
    if x not in seen:
        seen.add(header)
        print header

(您应该能够将其转换为Awk或Perl如果Python不是你的杯茶。)

(You should be able to translate this to Awk or Perl if Python is not your cup of tea.)

这篇关于如何看待包含文件的预处理后的实际顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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