首页 > 解决方案 > 如何使用 C 中的内部 c++ 类类型?

问题描述

我有一个MyClass声明公共枚举类型的 C++ 类,MyEnum我想在 C 文件中使用该枚举。我怎样才能做到这一点 ?

我试图在 C++ 文件中声明我的函数,然后将所有内容都设置为extern "C",但遗憾的是我使用了一些定义在其中的函数,big_hugly_include.h并且这个头文件不喜欢被包含在内external "C"(它给了我一个template with C linkage错误)。

我不能(不想)改变这个包含,我需要它,因为它定义了my_function_from_big_include. 我卡住了吗?


my_class_definition.h

class MyClass
{
public:
   // I would like to keep it that way as it is mainly used in C++ files
   typedef enum
   {
      MY_ENUM_0,
      MY_ENUM_1,
      MY_ENUM_2
   } MyEnum;
};

尝试 1 :my_c_function_definition.c :

#include "my_class_definition.h"

// I cannot remove this header
#include "big_hugly_include.h"

// foo is called in other C files
void foo()
{
   // I need to call this function with the enum from the C++ class
   // This doesn't work (class name scope does not exist in C)
   my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}

尝试 2 :my_c_function_definition.cpp :

#include "my_class_definition.h"

extern "C"
{

// Error template with C linkage
#include "big_hugly_include.h"

// foo is called in other C files
void foo()
{
   // That would be ideal
   my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}

// end of extern "C"
}

编辑以回应@artcorpse

尝试 3 :my_c_function_definition.cpp :

#include "my_class_definition.h"

// Error multiple definition of [...]
// Error undefined reference to [...]
#include "big_hugly_include.h"

extern "C"
{
// foo is called in other C files
void foo()
{
   // That would be ideal
   my_function_from_big_include(MyClass::MyEnum::MY_ENUM_0);
}

// end of extern "C"
}

标签: c++ctypescalling-conventioncross-language

解决方案


我想在 C 文件中使用该枚举。我怎样才能做到这一点?

C++ 中的枚举概念源自 C,因此您唯一需要做的就是将此枚举与 C 不知道的纯 cpp API 隔离开来(请记住名称修饰,见下文)。

由于 C 在类/结构枚举中不知道你不能使用它们。您必须定义全局范围枚举或创建将映射 C++ 特定枚举的此类枚举。

因此,应在共享 API 所在的位置创建单独的头文件。做这样的事情:

// shared C, C++ header
#ifdef __cplusplus
extern "C" 
{
#endif

enum YourMagicEnum {
    YourMagicEnumAValue,
    YourMagicEnumBValue,
    YourMagicEnumCValue,
};

void someFunction(YourMagicEnum x);

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

现在extern "C"只需要禁用名称修饰的函数(在 C++ 中,您可以进行函数重载,因此编译器生成包含参数类型信息的名称)。

在定义这样的函数时,它还应该extern "C"在该定义的前面有 。

请记住,在该标题中只能放置 C 特定的特性和功能。

还要记住 VLA(可变长度数组)是 C 标准,但不是 C++ 标准(大多数编译器支持 C++ 的 VLA)。

有关详细信息,请参阅此页面


推荐阅读