外部"C"的作用是什么?在C ++中? [英] What is the effect of extern "C" in C++?

查看:144
本文介绍了外部"C"的作用是什么?在C ++中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

extern "C"放入C ++代码中究竟能做什么?

What exactly does putting extern "C" into C++ code do?

例如:

extern "C" {
   void foo();
}

推荐答案

外部"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 (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.

由于C ++重载了函数名,而C没有重载,因此C ++编译器不能仅使用函数名作为要链接的唯一ID,因此它通过添加有关自变量的信息来破坏名称. AC编译器不需要处理名称,因为您不能在C中重载函数名称.当您声明函数在C ++中具有外部"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.

您知道,您可以为每个单独的声明/定义指定"C"链接,也可以使用一个块将声明/定义序列分组以具有一定的链接:

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;
}

如果您关心技术方面的问题,它们会在C ++ 03标准的7.5节中列出,这是一个简短的摘要(重点是外部"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" 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

这篇关于外部"C"的作用是什么?在C ++中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆