用 C++ 文件编译 C 共享对象
Compile C shared object with C++ files
我有一个 C 共享对象(.so 文件),我只能使用 gcc 编译它,因为它只使用像 strcpy_s.
这样的 C 函数
我有一个 C++ 代码,其中包含一些仅 C++ 的库。
是否可以同时使用 gcc 编译共享对象和使用 g++ 编译我的代码?
当然,您可以 link 您的 C++ 程序与您共享的 C 库。只需确保通过在 C 库头文件中的函数周围添加 extern "C" { ... }
来告诉 C++ 编译器该库中的函数具有 C linkage:
shared_c_lib.h
#ifdef __cplusplus
extern "C" {
#endif
// all your C functions declarations/prototypes
#ifdef __cplusplus
} // extern "C"
#endif
我有一个 C 共享对象(.so 文件),我只能使用 gcc 编译它,因为它只使用像 strcpy_s.
这样的 C 函数我有一个 C++ 代码,其中包含一些仅 C++ 的库。
是否可以同时使用 gcc 编译共享对象和使用 g++ 编译我的代码?
当然,您可以 link 您的 C++ 程序与您共享的 C 库。只需确保通过在 C 库头文件中的函数周围添加 extern "C" { ... }
来告诉 C++ 编译器该库中的函数具有 C linkage:
shared_c_lib.h
#ifdef __cplusplus
extern "C" {
#endif
// all your C functions declarations/prototypes
#ifdef __cplusplus
} // extern "C"
#endif