预处理后如何查看包含文件的实际顺序?
我有一个 .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?
我问的原因是因为我需要以正确的顺序将我的头文件包含在 SWIG 接口文件中,以避免生成奇怪的 SWIGTYPE_p_* 包装器.
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 手册.
See the GNU cpp manual for details.
编辑这是一个小的 Python 脚本,它将显示包含顺序,假设所有标头都有包含保护:
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
(如果你不喜欢 Python,你应该可以把它翻译成 Awk 或 Perl.)
(You should be able to translate this to Awk or Perl if Python is not your cup of tea.)
相关文章