在C code。使用C ++库 [英] Using C++ library in C code

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

问题描述

我有一个C ++库,提供各种类别的管理数据。我源$ C ​​$ C的库。

我要扩展C ++ API来支持C函数调用,使得库可以与C code和C ++ code同时使用。

我使用GNU工具链(GCC,glibc的,等等),所以语言和架构的支持不是问题。

有什么原因,这就是技术上不可能?

是否有任何的疑难杂症的,我需要注意的?

是否有资源,例如:code和/或文档提供有关本?


这是我已经发现了一些其他的事情:


  1. 使用下面的包你的C ++需要由C code中使用的标题。

 的#ifdef __cplusplus
为externC{
#万一
//
// code在这儿... ...
//
#IFDEF __cplusplus
} //为externC
#万一

<醇开始=2>

  • 保留真正的C ++在单独的头文件不是由C.包括接口想想 PIMPL原则在这里。使用的#ifndef __cplusplus #ERROR 的东西在这里可以检测任何疯狂。

  • 细心的C ++标识符在C code姓名

  • 枚举在C和C ++编译器之间的规模不等。也许不是,如果您使用GNU工具链,不过,要注意的一个问题。

  • 有关结构遵循下列表格,以便C不混淆。

      typedef结构X {...}×


  • 然后使用指针绕过C ++对象,他们只是在C语言中可以声明为结构X其中X是C ++对象。


  • 这一切是朋友谁在C ++是一个向导礼貌。


    解决方案

    是的,这当然是可能的。您将需要编写C ++与的externC声明功能的接口层:

     的externC诠释富(字符*巴)
    {
        返回realFoo(标准::字符串(巴));
    }

    然后,您将调用富()从C模块,它会通过调用到 realFoo()这是在C ++实现的功能。

    如果您需要公开的数据成员和方法一个完整的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 code。使用C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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