How to see the actual order of include files after preprocessing?(预处理后如何查看包含文件的实际顺序?)
问题描述
我有一个 .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.)
这篇关于预处理后如何查看包含文件的实际顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:预处理后如何查看包含文件的实际顺序?
- C语言详解float类型在内存中的存储方式 2023-03-27
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- Easyx实现扫雷游戏 2023-02-06
- Qt计时器使用方法详解 2023-05-30
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言qsort()函数的使用方法详解 2023-04-26
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- ubuntu下C/C++获取剩余内存 2023-09-18