5

I'm using a header only library and would like to include only the part of the library that I'm actually using.

If I include one of the library's headers how can I find all the other headers that are included by the library? Or phrased more generally: how can I find all files that make up a C++ translation unit? (I'm using g++ on Linux.)

Edit: Possible approaches with gcc (answers summary)

  • gcc -H produces output of the form:

    ... /usr/include/c++/4.6/cmath
    .... /usr/include/math.h
    ..... /usr/include/x86_64-linux-gnu/bits/huge_val.h
    ..... /usr/include/x86_64-linux-gnu/bits/huge_valf.h
    

    You have to filter out the system headers manually, though.

  • gcc -E gives you the raw preprocessor output:

    # 390 "/usr/include/features.h" 2 3 4
    # 38 "/usr/include/assert.h" 2 3 4
    # 68 "/usr/include/assert.h" 3 4
    extern "C" {    
    extern void __assert_fail (__const char *__assertion, __const char *__file,
          unsigned int __line, __const char *__function)
          throw () __attribute__ ((__noreturn__));
    

    You have to parse the linemarkers manually. See: http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html

  • gcc -M produces a Make file for the given source file. The output looks as follows:

    object.o: mylibrary/object.h /usr/include/c++/4.6/map \
     /usr/include/c++/4.6/bits/stl_tree.h \
     /usr/include/c++/4.6/bits/stl_algobase.h \
     /usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h \
    
Bernhard Kausler
  • 5,119
  • 3
  • 32
  • 36
  • possible duplicate of [Do you know tool building tree of include files in project\file?](http://stackoverflow.com/questions/1226044/do-you-know-tool-building-tree-of-include-files-in-project-file) – In silico Feb 04 '13 at 09:54

4 Answers4

4

I believe you're looking for gcc -H.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
3

g++ -M somefile to get a makefile that has all files somefile included as dependencies for somefile.o.

g++ -MM somefile the same, but doesn't list system headers (e.g. anything in /usr/include or /usr/local/include).

What do you need this for? If it's for dependency tracking, the above should suffice. If you, on the other hand, want to do some crazy thing like "I include this header and that header includes this header, so I don't need to include it again" - don't. No, seriously. Don't.

Cubic
  • 14,902
  • 5
  • 47
  • 92
2

You could obtain the preprocessed source using GCC's -E flag, then grep it for #include. I'm not aware of any other way for source files to find their way into a translation unit, so this should do the job.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks for the hint! Unfortunately the "#include"s are gone after the preprocessor. But you can recover that information from the linemarkers inserted by the preprocessor. See: http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html – Bernhard Kausler Feb 04 '13 at 10:25
  • Could you please update your answer accordingly so that I can accept it? :) – Bernhard Kausler Feb 04 '13 at 10:26
2

With g++ (and most Unix compilers, I think), you can use -M. For other compilers, you'll need -E or /E, capture the output in a file, and post-process the file using your favorite scripting language. (I do this in my makefiles, to build the dependencies.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329