应外部" C"附上一个C ++函数的声明或定义? [英] Shall external "C" enclose the declaration or definition of a C++ function?

查看:151
本文介绍了应外部" C"附上一个C ++函数的声明或定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个CPP文件中看到外部C{...} 围住的几个函数的定义。

从<一个href=\"https://isocpp.org/wiki/faq/mixing-c-and-cpp\">https://isocpp.org/wiki/faq/mixing-c-and-cpp,我想在CPP文件中使用的externC的目的是使可在C程序中使用的封闭C ++函数。

在链接的例子显示了的externC包围的C ++函数而已,而不是他们的定义

的声明

  

刚宣布C ++函数externC(在你的C ++ code)和呼叫
  它(从C或C ++ code)。例如:

  // C ++ code:
    为externC无效F(INT);
    无效F(int i)以
    {
        // ...
    }


我在开始时提到的CPP文件看起来像来代替:

  // C ++ code:
    为externC{    无效F(int i)以
    {
        // ...
    }    无效克(int i)以
    {
        // ...
    }    }

谈情的externC包围的C ++函数的声明或者定义?
如果是这样,为什么?


解决方案

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

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

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


请注意:

由于的externC{} 这是无效的C语法,使该C编译器的工作,你需要在<$使用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 ++的东西
     }
 }

请注意,该声明没有包括在这里。这可以用来作为一种技术,当你想覆盖从库中暴露<一个功能特别有用href=\"http://stackoverflow.com/questions/15525537/what-are-practical-applications-of-weak-linking\">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.

这篇关于应外部&QUOT; C&QUOT;附上一个C ++函数的声明或定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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