简体   繁体   English

如何查找构成C ++中翻译单元的所有文件?

[英]How to find all files that constitute a translation unit in C++?

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? 或更笼统地说:我如何找到组成C ++转换单元的所有文件? (I'm using g++ on Linux.) (我在Linux上使用g ++。)

Edit: Possible approaches with gcc (answers summary) 编辑:gcc的可能方法(答案摘要)

  • gcc -H produces output of the form: gcc -H产生以下形式的输出:

     ... /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: gcc -E给您原始的预处理输出:

     # 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 请参阅: http : //gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html

  • gcc -M produces a Make file for the given source file. gcc -M为给定的源文件生成一个Make文件。 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 \\ 

我相信您正在寻找gcc -H

g++ -M somefile to get a makefile that has all files somefile included as dependencies for somefile.o. g++ -M somefile获取一个makefile,该makefile包含所有文件somefile作为somefile.o的依赖项。

g++ -MM somefile the same, but doesn't list system headers (eg anything in /usr/include or /usr/local/include ). g++ -MM somefile相同,但是没有列出系统头文件(例如/usr/include/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. 别。

You could obtain the preprocessed source using GCC's -E flag, then grep it for #include . 您可以使用GCC的-E标志获取预处理的源,然后将其grep表示为#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. 我不知道源文件可以通过其他任何方式找到进入翻译单元的方式,因此这应该可以完成工作。

With g++ (and most Unix compilers, I think), you can use -M . 对于g ++(我认为是大多数Unix编译器),可以使用-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. 对于其他编译器,您将需要-E/E ,将输出捕获到文件中,然后使用您喜欢的脚本语言对文件进行后处理。 (I do this in my makefiles, to build the dependencies.) (我在我的makefile中执行此操作,以建立依赖关系。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM