How to see the actual order of include files after preprocessing?

夙愿已清 提交于 2019-11-30 14:04:46

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!