【问题标题】:How to use internal c++ class types from C? [duplicate]如何使用 C 中的内部 c++ 类类型? [复制]
【发布时间】:2019-03-27 07:53:05
【问题描述】:

我有一个 C++ 类 MyClass,它声明了一个公共枚举类型 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 中访问它。
  • 除了将枚举移动/复制到全局命名空间之外,别无他法
  • 为什么 typedef enum ... 在 C++ 中?
  • 您的 .h 不是真正的 .h,因为至少缺少 classstruct;。您还有其他遗漏的内容吗?
  • 为关闭此答案而提供的副本恕我直言不合适,因为它没有解决类型问题。

标签: c++ c types calling-convention cross-language


【解决方案1】:

我想在 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)。

欲了解更多信息see this page

【讨论】:

  • 我可能最终会这样做,但是我只有一个使用此类的 C 文件,其他所有内容都是 C++,我想将所有内容保留在类命名空间中以避免我的所有枚举定义都是全局的(我真的有很多,很快就会很乱)。
  • C 中没有命名空间,也没有类,所以如果需要访问 C 的 API 形式,则不能使用它们。
  • 我明白这一点,但是有没有办法定义一个 C 类型,它是我类中的 C 类型的副本?
  • 只有一些脚本会解析原始文件并生成相应的 C 头文件。
  • 丑陋的。好的,我想我会手动完成,希望当代码发展到更多的 C++ 时,我们会摆脱它。我将再等几个小时,看看其他人是否提供了另一种解决方法,否则我认为你的答案更有意义。如果您可以使用与我的示例相同的命名来重写您的答案,并展示如何将此类型声明包含回类中,那将使其完美!
【解决方案2】:

您的 Try2 非常接近解决方案。尝试将包含移到外部“C”之外。 我通常只是单独标记每个功能:

extern "C" void foo() 
{
...
}

这样做的好处是将一个符号导出为 C 符号,而不是尝试转换所有内容。

【讨论】:

  • 这是一个很好的建议,但遗憾的是我在这样做时遇到了链接错误(Undefined referencemultiple definition)。你知道什么可以创造它(big_hugly_include.h 真的充满了****)?我在我的问题中添加了 try 3。
  • 不幸的是,如果不了解这个 big_hugly_include.h,我认为我无法提供太多见解。这听起来像是一个 C++ 头文件,因此它最重要的可能是它位于 extern "C" 块之外。您提到的两种类型的错误听起来像是链接错误,因此您的其他一些 cpp 文件必须不匹配。您是否收到任何与“_foo”相关的错误?
  • 没有与 _foo 相关的错误,但我怀疑这包含混合了 c 和 cpp 头文件。
猜你喜欢
  • 2014-01-07
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 2011-06-08
  • 2013-05-06
  • 2010-12-07
相关资源
最近更新 更多