在 C 代码中使用 C++ 库

Using C++ library in C code(在 C 代码中使用 C++ 库)

本文介绍了在 C 代码中使用 C++ 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 库,它提供了各种用于管理数据的类.我有库的源代码.

我想扩展 C++ API 以支持 C 函数调用,以便库可以同时与 C 代码和 C++ 代码一起使用.

我使用的是 GNU 工具链(gcc、glibc 等),因此语言和架构支持不是问题.

是否有任何理由说明这在技术上是不可能的?

是否有任何问题需要我注意?

是否有与此相关的资源、示例代码和/或文档?

<小时>

我发现的其他一些事情:

  1. 使用以下内容来包装 C 代码需要使用的 C++ 标头.

#ifdef __cplusplus外部C"{#万一////代码在这里...//#ifdef __cplusplus}//外部 "C"#万一

  1. 将真正的"C++ 接口保存在 C 不包含的单独头文件中.想想 PIMPL 原则这里.使用 #ifndef __cplusplus #error 的东西有助于在这里检测任何疯狂.
  2. 小心将 C++ 标识符作为 C 代码中的名称
  3. 枚举在 C 和 C++ 编译器之间的大小不同.如果您使用的是 GNU 工具链,则可能不是问题,但仍然要小心.
  4. For structs 遵循以下形式,以免 C 混淆.

    typedef struct X { ... } X

  5. 然后使用指针来传递 C++ 对象,它们只需要在 C 中声明为 struct X,其中 X 是 C++ 对象.

所有这些都是由一位 C++ 高手朋友提供的.

解决方案

是的,这当然是可能的.您将需要用 C++ 编写一个接口层,用 extern "C" 声明函数:

extern "C" int foo(char *bar){return realFoo(std::string(bar));}

然后,您将从 C 模块调用 foo(),该模块会将调用传递给在 C++ 中实现的 realFoo() 函数.>

如果您需要公开具有数据成员和方法的完整 C++ 类,那么您可能需要做比这个简单的函数示例更多的工作.

I have a C++ library that provides various classes for managing data. I have the source code for the library.

I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.

I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.

Are there any reasons why this is technically not possible?

Are there any gotcha's that I need to watch out for?

Are there resources, example code and/or documentation available regarding this?


Some other things that I have found out:

  1. Use the following to wrap your C++ headers that need to be used by C code.

#ifdef __cplusplus
extern "C" {  
#endif  
//  
// Code goes here ...  
//  
#ifdef __cplusplus  
} // extern "C"  
#endif

  1. Keep "real" C++ interfaces in separate header files that are not included by C. Think PIMPL principle here. Using #ifndef __cplusplus #error stuff helps here to detect any craziness.
  2. Careful of C++ identifiers as names in C code
  3. Enums varying in size between C and C++ compilers. Probably not an issue if you're using GNU tool chain, but still, be careful.
  4. For structs follow the following form so that C does not get confused.

    typedef struct X { ... } X
    

  5. Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.

All of this is courtesy of a friend who's a wizard at C++.

解决方案

Yes, this is certainly possible. You will need to write an interface layer in C++ that declares functions with extern "C":

extern "C" int foo(char *bar)
{
    return realFoo(std::string(bar));
}

Then, you will call foo() from your C module, which will pass the call on to the realFoo() function which is implemented in C++.

If you need to expose a full C++ class with data members and methods, then you may need to do more work than this simple function example.

这篇关于在 C 代码中使用 C++ 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 C 代码中使用 C++ 库