且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C ++源代码,是什么对外部&QUOT的效果; C"?

更新时间:2023-02-15 21:02:07

为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链接

  • 应仅在命名空间范围内发生链接规范

  • 所有函数类型,函数名和变量名有语言联动行使> See理查德的评论: 只有函数名和变量名具有外部链接有一个语言链接

  • 两个函数类型有不同的语言之间的联系是不同的类型,即使否则相同

  • 联动规范巢,内层确定最终的联动

  • 的externC为类成员忽略

  • 在一个特定的名字最多一个函​​数可以有(的空间不​​分),C链接

  • 的externC强制函数有外部链接(不能让它静态)击> 见理查德的评论:的'静态'里面'的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
  • all function types, function names and variable names have a language linkage 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)
  • extern "C" forces a function to have external linkage (cannot make it static) 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