三生石@ 2009-06-25 02:10 采纳率: 100%
浏览 313
已采纳

在 c + + 中外部"c"的影响是什么?

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

For example:

extern "C" {
   void foo();
}

转载于:https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c

  • 写回答

13条回答 默认 最新

  • YaoRaoLov 2009-06-25 02:12
    关注

    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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境