且构网

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

应外部" C"附上一个C ++函数的声明或定义?

更新时间:2022-12-08 18:47:43

应该括在头文件中的声明和定义应封闭,只要翻译单元使用的是C ++编译器编译,只要申报WASN 没见到那里。结果
这是从来没有错的C ++ code做两个。

如果C编译器来编译函数定义,这是没有必要的(或者我应该不如说是错误的语法,请参见下面的注释)。

的externC{} 范围控制纯C符号链接用于里面的一切。否则 C ++名称重整 将适用。


请注意:

由于的externC{} 这是无效的C语法,使该C编译器的工作,你需要在 #IFDEF :

MyHeader.h

 的#ifdef __cplusplus
 为externC{
 #万一 // ... C风格的函数名声明
 无效美孚(INT I); #IFDEF __cplusplus
 } //为externC
 #万一


使用该中的externC{} 范围实际上是双重的:


导出C ++ code到C

如果上面是用C编译器编译,它出现它作为一个普通的C函数声明。如果编译C ++编译器的的extern 关键字适用,而C ++名称重整将是燮pressed。

关于定义,函数可以使用任何C ++特性它的定义中:

 的externC{
     无效美孚(INT X){
         的std ::矢量V(X);
         // ...等等,更多的C ++的东西
     }
 }

请注意,该声明没有包括在这里。这可以用来作为一种技术,当你想覆盖从库中暴露weak联动的。

在包括 MyHeader.h ,可以省略的externC{} 范围的情况。


从C导入C code ++

如果上述声明是C ++编译器看到的那样,再次C ++名称重整是燮pressed,任何调用参考富()西港岛线被解决使用纯C函数符号名称的链接:

 的#includeMyHeader.h
  MyClass类{
  上市:
       无效杆(int y)对{
           //使用富()作为普通的C函数:
           富(Y);
       }
  };

富()功能实现从使用C编译器创建一个目标文件(或存档)提供。

I saw in a cpp file that external "C" {...} encloses the definitions of several functions.

From https://isocpp.org/wiki/faq/mixing-c-and-cpp, I guess the purpose of using extern "C" in the cpp file is to make the enclosed C++ functions available to be used in a C program.

The example in the link shows that extern "C" encloses the declarations of the C++ functions only, not their definitions

Just declare the C++ function extern "C" (in your C++ code) and call it (from your C or C++ code). For example:

    // C++ code:
    extern "C" void f(int);
    void f(int i)
    {
        // ...
    }

The cpp file I mentioned at the beginning looks like instead:

    // C++ code:
    extern "C" {

    void f(int i)
    {
        // ...
    }

    void g(int i)
    {
        // ...
    }

    }

Shall extern "C" enclose the declarations or definitions of C++ functions? If so, why?

It should enclose the declarations in the header file, and definitions should be enclosed as long the translation unit is compiled using the c++ compiler, and as long the declaration wasn't seen there.
It's never wrong doing both in c++ code.

If the c compiler is used to compile the function definitions, it's not necessary (or should I better to say would be wrong syntax, see the note below).

extern "C" {} scopes control that plain c symbols linkage is used for everything inside. Otherwise c++ name mangling would be applied.


Note:

Since extern "C" {} this isn't valid c syntax, to make that working with the c compiler, you'll need to use it within #ifdef:

MyHeader.h:

 #ifdef __cplusplus
 extern "C" {
 #endif

 // ... c style function name declarations
 void foo(int i);

 #ifdef __cplusplus
 } // extern "C"
 #endif


The use of the extern "C" {} scope is actually twofold:


Exporting C++ code to C

If the above is compiled with the c compiler, it appears for it as a normal c function declaration. If compiled with the c++ compiler the extern keyword applies and the c++ name mangling will be suppressed.

Regarding the definition, the function can use any c++ features inside it's definition:

 extern "C" {
     void foo(int x) {
         std::vector v(x);
         // ... blah, more c++ stuff
     }
 }

Note that the declaration wasn't included here. This can be used as a technique, particularly useful when you want to override functions exposed from a library for weak linkage.

In case of including the MyHeader.h, the extern "C" {} scope can be omitted.


Importing C code from C++

If the above declaration is seen in the c++ compiler, again c++ name mangling is suppressed, and any call reference to foo() wil be resolved by the linker using a plain c function symbol name:

  #include "MyHeader.h"
  class MyClass {
  public:
       void bar(int y) {
           // Use foo() as plain c function:
           foo(y);
       }
  };

The foo() function implementation is provided from an object file (or archive) that was created using the c compiler.