Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C程序链接到C++;共享库 我有共享的C++语言库,它提供了一些用C++编写的不同应用程序的API调用,现在我想在C程序中使用这个库。原始库包含的数据类型仅对C++有效,如STD::String和STD::vector如下: typedef u_int32_t ApplicationID; typedef std::string IPAddress; typedef std::vector<int> SDLIST; typedef char* IPAddress; typedef int* SDLIST;_C++_C_Boost_Shared Memory - Fatal编程技术网

将C程序链接到C++;共享库 我有共享的C++语言库,它提供了一些用C++编写的不同应用程序的API调用,现在我想在C程序中使用这个库。原始库包含的数据类型仅对C++有效,如STD::String和STD::vector如下: typedef u_int32_t ApplicationID; typedef std::string IPAddress; typedef std::vector<int> SDLIST; typedef char* IPAddress; typedef int* SDLIST;

将C程序链接到C++;共享库 我有共享的C++语言库,它提供了一些用C++编写的不同应用程序的API调用,现在我想在C程序中使用这个库。原始库包含的数据类型仅对C++有效,如STD::String和STD::vector如下: typedef u_int32_t ApplicationID; typedef std::string IPAddress; typedef std::vector<int> SDLIST; typedef char* IPAddress; typedef int* SDLIST;,c++,c,boost,shared-memory,C++,C,Boost,Shared Memory,但在C中,我们没有字符串或向量,这两种数据类型应修改如下: typedef u_int32_t ApplicationID; typedef std::string IPAddress; typedef std::vector<int> SDLIST; typedef char* IPAddress; typedef int* SDLIST; 我尝试在代码中进行以下更改: typedef u_int32_t

但在C中,我们没有字符串或向量,这两种数据类型应修改如下:

typedef u_int32_t           ApplicationID;
typedef std::string         IPAddress;
typedef std::vector<int>    SDLIST;
typedef char*   IPAddress;
typedef int*    SDLIST;
我尝试在代码中进行以下更改:

typedef u_int32_t           ApplicationID;
enter code here
#ifdef __cplusplus
    typedef std::string         IPAddress;
    typedef std::vector<int>    SDLIST;
#else
    typedef char*               IPAddress;
    typedef int*                SDLIST;
#endif


#ifdef __cplusplus
extern "C" {
#endif

    register_client(IPAddress ip);
    export(ApplicationID id, SDLIST *list);

#ifdef __cplusplus
}
#endif
typedef u_int32_t ApplicationID;
在这里输入代码
#ifdef_uucplusplus
typedef std::字符串IPAddress;
typedef std::向量SDLIST;
#否则
typedef char*IPAddress;
typedef int*SDLIST;
#恩迪夫
#ifdef_uucplusplus
外部“C”{
#恩迪夫
注册用户端(ip地址ip);
导出(应用程序id、SDLIST*列表);
#ifdef_uucplusplus
}
#恩迪夫
我的问题是:

这是一个正确的方法,可以在C++和

我的共享库使用Boost进程间库,它是标准POSIX共享内存调用的包装器。每当我尝试将此共享库链接到任何应用程序时,我都应该在应用程序中再次包含
lrt
。因此,我的问题是,是否可以将共享库静态链接到
lrt
库,而无需在使用我的共享库的所有应用程序中包含
lrt


让我逐一回答你的问题:

  • 显然,这取决于您的用例,您希望混合哪种代码。如果你想提供你的库应该从C和C++调用的能力,你的库接口应该是C兼容的。在C界面中寻找如何包装C++函数的答案。

  • 通常建议不要将所有依赖项打包到库中,因为它会生成大量二进制文件,并且会破坏共享库的用途,除非打包的依赖项很小。然而,在您的情况下,如果您想这样做,您需要创建静态共享库。但是,无法从两个共享库中创建。它们都需要对象文件。看看答案


  • 如果你想让它工作,你需要建立一个C++接口库,它实现了一个C语言的API,它将C数据类型转换成C++数据类型。特别是std::string不是char*,vector不是int*

    例如,如果API定义了一个C++函数,比如

    bool CPPAPIFunction( std::string str, std::vector<int> vec )
    
    bool cppapi函数(std::string str,std::vector vec)
    
    您需要实现一个包装器函数(编译并链接为C++),如

    int-myCMappingFunction(char*cstr、int*carray、int-arraylen)
    {
    std::字符串str(cstr);
    std::vec;
    对于(int i=0;i < ARARYLLN;I++)//…复制C数组到C++向量中
    返回(int)cppfapi函数(str,vec);
    }
    

    也不要忘记在一个外部“C”块中声明你的包装器函数,这样名字的名字将是C风格而不是C++。

    除非你在C中编译了库,包含的头和基础的定义将不匹配。相反,只需将C更改为可调用的接口,或者添加一个新的头和函数,“包装”C++代码,并是C-Cabable。为什么要用两种语言编写它?您可以创建C库并为其编写C++绑定,或者编写C++库并为其编写C绑定。然后将该库与您的应用程序链接,瞧。导出的函数和DLL的数据类型应为C样式。谢谢您的回答,但就我的情况而言,我不希望应用程序知道我对共享内存的使用情况,这与他无关。是的……没错。为此,您需要实现包装器函数来实现这一点。例如,您将需要具有接受和返回C数据类型而不是C++数据类型的包装器函数。