Possible Duplicate:
Why do we need extern “C”{ #include <foo.h> } in C++?
我经常看到这样编码的程序:
extern "C" bool doSomeWork() {
//
return true;
}
为什么我们使用 extern "C"
block ?我们可以用 C++ 中的东西替换它吗?使用 extern "C"
有什么好处吗?
我确实看到了一个解释 this 的链接但是既然我们已经有了 C++,为什么还要用 C 编译一些东西呢?
最佳答案
extern "C"使名称不被破坏。
在以下情况下使用:
我们需要在 C++ 中使用一些 C 库
extern "C" int foo(int);
我们需要将一些 C++ 代码导出到 C
extern "C" int foo(int) { something; }
我们需要能够解析共享库中的符号——所以我们需要摆脱重整
extern "C" int foo(int) { something; } /// typedef int (*foo_type)(int); foo_type f = (foo_type)dlsym(handle,"foo")
关于c++ - 何时在 C++ 中使用 extern "C"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1292138/