问题描述
到底是什么把的externC
成C ++ code呢?
例如:
的externC{
无效美孚();
}
为externC,使一个函数名在C ++中有'C'联动(编译器不会裂伤的名称),以便客户端C code能链接(即使用)使用一个包含你的函数只是声明中的C兼容的头文件的功能。你的函数定义包含在一个二进制格式的客户端'C'连接器将然后链接到使用C的名字(一个由你的C ++编译器编译)。
由于C ++有重载函数名和C不,C ++编译器不能只使用函数名作为一个独特的ID链接,所以它通过增加有关参数的信息轧液的名称。 C编译器并不需要裂伤的名字,因为当你指出一个函数的externC链接在C ++中,你不能超载在C函数名,C ++编译器不参数/参数类型信息添加到用于名称联动。
只要你知道,你可以明确指定C链接到每一个人声明/定义或使用块组声明/定义序列有一定的联系:
的externC无效美孚(INT);
为externC
{
无效克(炭);
INT I;
}
如果你关心的技术问题,他们在节中的C ++标准03 7.5上市,这里是一个简要的总结(重点为externC):
- 的externC是一个联动规范
- 每个编译器的需要的提供C链接
- 应仅在命名空间范围内发生链接规范
- 见理查德的评论:的'静态'里面'的externC'是有效的;因此声明的实体有内部连接,因此不具有语言链接
- 联动从C ++到其他语言,并从其他语言在C ++中定义的对象定义的对象是实现定义与语言相关的。只有在两个语言实现的对象布局策略是类似足够可以这样的联动实现
What exactly does putting extern "C"
into C++ code do?
For example:
extern "C" {
void foo();
}
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 (i.e 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.
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.
Just so you know, you can specify "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;
}
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" is a linkage-specification
- Every compiler is required to provide "C" linkage
- a linkage specification shall occur only in namespace scope
- See 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: 'static' inside 'extern "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
这篇关于在C ++源代码,是什么对外部&QUOT的效果; C"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!