如何判断哪个#include提供了什么对象?

时间:2014-09-11 00:28:49

标签: c++ c include

有没有办法告诉Foo来自哪里,而不是每次#include

#include "Baz.h"
#include "Quuz.h"

int main() {
  Foo n;
}

3 个答案:

答案 0 :(得分:6)

我认为最简单的方法也是最愚蠢的方式:再次定义并获得错误

#include <iostream>
using namespace std;

typedef int cout; //<--- just for test

int main(){
  cout<<"Hi!"<<endl;
  return 0;
}

现在编译时(使用gcc)我遇到了错误:

 tt.cpp:8: error: reference to ‘cout’ is ambiguous 
 tt.cpp:5: error: candidates are: typedef int cout
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:59: error: std::ostream std::cout

答案 1 :(得分:3)

是。预处理源代码而不编译它。使用GCC或Clang这意味着在“编译”时将-c替换为-E(并且可能更改传递给-o的输出文件名。然后您将拥有一个包含所有#includes的完整扩展文件,并且可以grep那个文件,看看Foo来自哪里。

答案 2 :(得分:1)

您可以使用cscope

构建代码:

$ cscope -R

然后选择:

Find this global definition: Foo

它将显示您定义的文件Foo