我正在使用仅限标头的库,并且只想包含我实际使用的库的一部分。
如果我包含一个库的头文件,我怎样才能找到库中包含的所有其他头文件?或者更一般地说:如何找到构成C ++翻译单元的所有文件? (我在Linux上使用g ++。)
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
但您必须手动过滤掉系统标题。
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__));
您必须手动解析线标记。请参阅:http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
gcc -M为给定的源文件生成Make文件。输出如下:
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 \
答案 0 :(得分:2)
我相信你正在寻找gcc -H。
答案 1 :(得分:2)
g++ -M somefile
获取一个makefile,其中包含somefile所包含的所有文件作为somefile.o的依赖项。
g++ -MM somefile
相同,但未列出系统标头(例如/usr/include
或/usr/local/include
中的任何内容)。
你需要什么?如果它是依赖性跟踪,上面应该就足够了。另一方面,如果你想做一些疯狂的事情,比如“我包含这个标题,那个标题包括这个标题,所以我不需要再包括它” - 不要。不,真的。不。
答案 2 :(得分:1)
您可以使用GCC的-E
标志获取预处理的源,然后将其grep用于#include
。我不知道源文件有任何其他方式可以找到他们进入翻译单元的方式,所以这应该可以胜任。
答案 3 :(得分:1)
使用g ++(我认为是大多数Unix编译器),您可以使用-M
。对于其他编译器,您需要-E
或/E
,捕获文件中的输出,并使用您喜欢的脚本语言对文件进行后处理。 (我在makefile中执行此操作,以构建依赖项。)