为什么要使用“extern "C++"”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/610934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would you use 'extern "C++"'?
提问by Les
回答by Thomas L Holaday
The language permits:
语言允许:
extern "C" {
#include "foo.h"
}
What if foo.h contains something which requires C++ linkage?
如果 foo.h 包含需要 C++ 链接的内容怎么办?
void f_plain(const char *);
extern "C++" void f_fancy(const std::string &);
That's how you keep the linker happy.
这就是您让链接器满意的方式。
回答by James Curran
There is no real reason to use extern "C++"
. It merely make explicit the linkage that is the implicit default. If you have a class where some members have extern "C" linkage, you may wish the explicit state that the others are extern "C++".
没有真正的理由使用extern "C++"
. 它只是明确了作为隐式默认值的链接。如果您有一个类,其中某些成员具有 extern "C" 链接,您可能希望其他成员是 extern "C++" 的显式状态。
Note that the C++ Standard defines syntactically extern "anystring"
. It only give formal meanings to extern "C"
and extern "C++"
. A compiler vendor is free to define extern "Pascal"
or even extern "COM+"
if they like.
请注意,C++ 标准在语法上定义了extern "anystring"
. 它只给正规的含义extern "C"
和extern "C++"
。编译器供应商可以自由定义extern "Pascal"
或者即使extern "COM+"
他们喜欢。
回答by jeffm
I'm not sure why you would need to do it, but according to thisarticle from Sun, you can use extern "C++" inside a block of extern "C" to specify certain functions in a group of "C" functions have the native C++ linkage.
我不确定您为什么需要这样做,但是根据Sun 的这篇文章,您可以在 extern "C" 块中使用 extern "C++" 来指定一组 "C" 函数中的某些函数具有本机 C++ 链接。
extern "C" {
void f(); // C linkage
extern "C++" {
void g(); // C++ linkage
extern "C" void h(); // C linkage
void g2(); // C++ linkage
}
extern "C++" void k();// C++ linkage
void m(); // C linkage
}
回答by Johannes Schaub - litb
Two guesses:
两种猜测:
- If you are in a
extern "C"
block, you can get C++ language linkage again by specifying a nestedextern "C++"
. - It reserves
C++
linkage, because it's the document defining C++. Who is in a better position for definingC++
language linkage than it itself. It also provides for completeness. Same deal as withsigned/unsigned
.
- 如果你在一个
extern "C"
块中,你可以通过指定一个嵌套的extern "C++"
. - 它保留
C++
链接,因为它是定义 C++ 的文档。谁C++
比它本身更适合定义语言链接。它还提供完整性。与 相同的处理signed/unsigned
。
Read this answerthat explains extern "LanguageName"
(i.e GCC has extern "Java"
) aswell.
回答by Gopal Subramani
Extern "C" is answered by many. The use case for extern "C++" is when calling C++ library function in a C function. The sub-use case, that is relevant, is when linking a C++ library with a C source code with main function. Check this wiki pagefor more details:
许多人回答外部“C”。extern "C++" 的用例是在 C 函数中调用 C++ 库函数。相关的子用例是将 C++ 库与带有主函数的 C 源代码链接起来。查看此wiki 页面以获取更多详细信息:
回答by Pesto
C and C++ use different name mangling rules. Essentially, extern "C" tells the C++ compiler to name the function as C would name it.
C 和 C++ 使用不同的名称修改规则。本质上,extern "C" 告诉 C++ 编译器将函数命名为 C 命名的。
回答by JaredPar
The #1 reason I use extern "C" is to avoid C++'s name mangling rules. This is very important if you are working in a .Net language and want to PInvoke into a particular native function. The only way to do this is with name mangling disabled.
我使用 extern "C" 的第一个原因是避免 C++ 的名称修改规则。如果您使用 .Net 语言并希望 PInvoke 进入特定的本机函数,这非常重要。做到这一点的唯一方法是禁用名称修改。
回答by Edouard A.
This specify which link convention to use. Most languages know how to link with a "C" style function.
这指定要使用的链接约定。大多数语言都知道如何链接“C”风格的函数。
You need this in two cases :
在两种情况下你需要这个:
- A C - or other languages for that matter- program calling a function written in C++
- A C++ program calling a function written in C
- AC - 或其他语言 - 程序调用用 C++ 编写的函数
- 一个 C++ 程序调用一个用 C 编写的函数
Example :
例子 :
// declared in function.h
void f1(void);
Your C code - actually other languages are able to link with C function - will not be able to link to it because the name in the object table will use C++ convention.
您的 C 代码 - 实际上其他语言能够与 C 函数链接 - 将无法链接到它,因为对象表中的名称将使用 C++ 约定。
If you write
如果你写
extern "C" void f1(void);
Now the linking works because it uses C convention.
现在链接有效,因为它使用 C 约定。
回答by James Brooks
Short answer is that you can use extern C to tell the compiler not to use name-mangling. This means you can link together bits of C and C++ code in the same project.
简短的回答是您可以使用 extern C 告诉编译器不要使用名称修改。这意味着您可以将同一项目中的 C 和 C++ 代码链接在一起。
回答by James Brooks
extern "C" is used to say that a C++ function should have C linkage. What this means is implementation dependant, but normally it turns off C++ name-mangling (and so overloading and strict type checking). You use it when you have a C++ function you want to be called from C code:
extern "C" 用于表示 C++ 函数应该具有 C 链接。这意味着什么取决于实现,但通常它会关闭 C++ 名称修改(以及重载和严格类型检查)。当您想要从 C 代码调用 C++ 函数时,可以使用它:
extern "C" void Foo(); // can be called easily from C
As for extern "C++", I've never seen it in real code, though the C++ Standard allows it. I guess it is a no-op.
至于 extern "C++",我从未在实际代码中看到过它,尽管 C++ 标准允许它。我想这是一个无操作。