如何查看预处理后包含文件的实际顺序?

发布于 2024-10-21 12:43:10 字数 375 浏览 2 评论 0原文

我有一个 .cpp 文件,其中包含一些头文件。这些头文件也可能包含其他头文件。包含防护已就位,以防止包含同一文件两次。

知道每个文件只包含一次。有没有办法确定包含所有标头的最终顺序?

我尝试使用 gcc -E 来获取预处理器输出,但生成的代码似乎无法用于提取我想要的信息。有人可以帮忙吗?

编辑

我问的原因是因为我需要以正确的顺序将头文件包含在 SWIG 接口文件中,以避免生成奇怪的 SWIGTYPE_p_* 包装器。

更新

感谢您的回答。使用 cpp -H 似乎非常有用。但是,我无法找到方法来 grepsed 这些结果,以便以正确的顺序获取头文件的简单列表并且不重复。

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?

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?

Edit

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.

Update

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热血少△年 2024-10-28 12:43:10

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

$ 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 手册

编辑 这是一个小的 Python 脚本,它将显示包含的顺序,假设所有标头都有包含防护:(

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。)

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

See the GNU cpp manual for details.

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

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

拧巴小姐 2024-10-28 12:43:10

预处理器按顺序工作,您可以“轻松”手动跟随他的工作。

假设您有:

file.cpp

#include 'one.h'
#include 'two.h'

one.h

#include 'three.h'
#include 'header.h'

Three.h

#include 'four.h'

预处理器将包括 one.h,其中将包括 Three.h,其中将包括 four.h。现在我们返回到one.h,header.h将被包含,最后我们返回file.cpp,two.h将被包含。所以顺序将是

  1. one.h
  2. 三.h
  3. 四.h
  4. header.h
  5. 二.h

我不知道你为什么问这个,但我强烈建议你以任何类型的非确定性的方式编写标题包含顺序工作,否则你迟早会遇到一些问题。

The preprocessor works sequentially, you can "easily" follow his job by hand.

say you have :

file.cpp

#include 'one.h'
#include 'two.h'

one.h

#include 'three.h'
#include 'header.h'

three.h

#include 'four.h'

The preprocessor will include one.h, which will include three.h which will include four.h. Now we return to one.h and header.h will be included and finally we return to file.cpp and two.h will be included. So the order will be

  1. one.h
  2. three.h
  3. four.h
  4. header.h
  5. two.h

I don't know why you're asking this, but I strongly suggest you write your headers in a way that any kind of non-deterministic inclusion order work, otherwise you'll run in some problems sooner or later.

七分※倦醒 2024-10-28 12:43:10

如果你想让事情变得更时髦,你可以尝试看看 http ://www.boost.org/doc/libs/release/libs/preprocessor 东西...

从我的想法来看,我认为你应该能够在每个标头中构造一个特殊的值,这每个文件包含的增量。就像我说的,看看 Boost 的东西可能会给你一些想法。

If you want things to get more funky, you could try to have a look at the http://www.boost.org/doc/libs/release/libs/preprocessor stuff...

From the top of my head, I think you ought to be able to construct a special value inside each header, which increments for each file inclusion. Like i said, looking at the Boost-stuff might give you some ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文