当为extern&QUOT使用; C"在C ++? [英] When to use extern "C" in C++?
问题描述
可能重复:结果
我们为什么需要的extern“ C”的{ #包括LT&;&foo.h中GT; }在C ++?
我经常看到程序codeD这样的:
I have often seen programs coded like:
extern "C" bool doSomeWork() {
//
return true;
}
为什么要使用的externC
阻止?我们可以在C ++的东西代替吗?是否有任何优势,使用的externC
?
Why do we use an extern "C"
block? Can we replace this with something in C++? Is there any advantage to using extern "C"
?
I do see a link explaining this but why do we need to compile something in C when we already have C++?
推荐答案
为externC使得名未进行重整。
extern "C" makes names not mangled.
它适用于:
-
我们需要在C中使用一些C ++库
We need to use some C library in C++
extern "C" int foo(int);
我们需要输出一些C ++ code到C
We need export some C++ code to C
extern "C" int foo(int) { something; }
我们需要共享库来解决符号的能力 - 所以我们需要摆脱重整
We need an ability to resolve symbol in shared library -- so we need to get rid mangling
extern "C" int foo(int) { something; }
///
typedef int (*foo_type)(int);
foo_type f = (foo_type)dlsym(handle,"foo")
这篇关于当为extern&QUOT使用; C"在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!