zoukankan      html  css  js  c++  java
  • What is the effect of extern “C” in 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.

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

    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

    Just wanted to add a bit of info, since I haven't seen it posted yet.

    You'll very often see code in C headers like so:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // all of your legacy C code here
    
    #ifdef __cplusplus
    }
    #endif
    

    What this accomplishes is that it allows you to use that C header file with your C++ code, because the macro "__cplusplus" will be defined. But you can also still use it with your legacy C code, where the macro is NOT defined, so it won't see the uniquely C++ construct.

    Although, I have also seen C++ code such as:

    extern "C" {
    #include "legacy_C_header.h"
    }
    

    which I imagine accomplishes much the same thing.

    Not sure which way is better, but I have seen both.

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

  • 相关阅读:
    iis7 下配置 ASP.NET MVC 项目遇到的问题 (WIN7 64位 旗舰版 第一次配置站点)
    C# .NET 使用 NPOI 读取 .xlsx 格式 Excel
    C# .NET 使用 NPOI 生成 .xlsx 格式 Excel
    SQL和T-SQL之间的区别
    .net core的服务器模式和工作站模式
    Windows Mysql8 设置大小写敏感
    每个国家对应的语言Locale和国家代码对照表(转)
    IL指令集
    使用 nvm 管理不同版本的 node 与 npm
    SSIS 包部署 Package Store 后,在 IS 中可以执行,AGENT 执行却报错
  • 原文地址:https://www.cnblogs.com/Searchor/p/14024658.html
Copyright © 2011-2022 走看看