代码之家  ›  专栏  ›  技术社区  ›  Shirakawa42

用c++文件编译c共享对象

  •  0
  • Shirakawa42  · 技术社区  · 6 年前

    我有一个c共享对象(.so文件),我只能使用gcc编译它,因为它只使用strcpy_s这样的c函数。

    我有一个C++代码,其中包含一些仅限C++的库。

    可以用gcc编译共享对象吗?我的代码可以用g++一起编译吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ted Lyngmo    6 年前

    当然,你可以 链接 你的c++程序和你的共享c库。只需通过添加 extern "C" { ... } 关于C库头文件中的函数:

    共享库

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // all your C functions declarations/prototypes
    
    #ifdef __cplusplus
    } // extern "C"
    #endif