extern“C"的作用是什么?在 C++ 中? [英] What is the effect of extern "C" in C++?
问题描述
将 extern "C"
放入 C++ 代码中究竟有什么作用?
What exactly does putting extern "C"
into C++ code do?
例如:
extern "C" {
void foo();
}
推荐答案
extern "C"
使 C++ 中的函数名具有 C 链接(编译器不会破坏名称)以便客户端C 代码可以使用仅包含函数声明的 C 兼容头文件链接到(使用)您的函数.您的函数定义包含在二进制格式中(由您的 C++ 编译器编译),然后客户端 C 链接器将使用 C 名称链接到该格式.
extern "C"
makes a function-name in C++ have C linkage (compiler does not mangle the name) so that client C code can link to (use) your function using a C compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client C linker will then link to using the C name.
由于 C++ 有函数名的重载而 C 没有,C++ 编译器不能只使用函数名作为唯一的 id 来链接,所以它通过添加有关参数的信息来破坏名称.AC 编译器不需要修改名称,因为您不能在 C 中重载函数名称.当您声明一个函数在 C++ 中具有 extern "C"
链接时,C++ 编译器不会添加参数/用于链接的名称的参数类型信息.
Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C"
linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.
正如您所知,您可以显式指定 extern "C"
链接到每个单独的声明/定义,或者使用块将一系列声明/定义分组以具有特定链接:
Just so you know, you can specify extern "C"
linkage to each individual declaration/definition explicitly or use a block to group a sequence of declarations/definitions to have a certain linkage:
extern "C" void foo(int);
extern "C"
{
void g(char);
int i;
}
如果您关心技术细节,它们在 C++03 标准的第 7.5 节中列出,这里是一个简短的总结(重点是 extern C"
):
If you care about the technicalities, they are listed in section 7.5 of the C++03 standard, here is a brief summary (with emphasis on extern "C"
):
extern "C"
是一个链接规范- 每个编译器都要求提供C"联动
- 链接规范只能出现在命名空间范围内
所有函数类型、函数名和变量名都有语言链接参见Richard的评论:只有具有外部链接的函数名和变量名才有语言链接- 具有不同语言链接的两种函数类型是不同类型,即使在其他方面相同
- 联动规格嵌套,内部决定最终联动
extern "C"
被类成员忽略- 至多一个具有特定名称的函数可以有C"链接(不考虑命名空间)
见理查德的评论:extern "C"
强制函数具有外部链接(不能使其成为静态)static
extern "C"
内有效;如此声明的实体具有内部链接,因此没有语言链接- 从 C++ 到用其他语言定义的对象以及从其他语言到用 C++ 定义的对象的链接是实现定义和语言相关的.只有两种语言实现的对象布局策略足够相似,才能实现这种联动
extern "C"
is a linkage-specification- Every compiler is required to provide "C" linkage
- A linkage specification shall occur only in namespace scope
All function types, function names and variable names have a language linkageSee Richard's Comment: Only function names and variable names with external linkage have a language linkage- Two function types with distinct language linkages are distinct types even if otherwise identical
- Linkage specs nest, inner one determines the final linkage
extern "C"
is ignored for class members- At most one function with a particular name can have "C" linkage (regardless of namespace)
See Richard's comment:extern "C"
forces a function to have external linkage (cannot make it static)static
insideextern "C"
is valid; an entity so declared has internal linkage, and so does not have a language linkage- Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved
这篇关于extern“C"的作用是什么?在 C++ 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!