我有一个C共享库(.so文件),我只能使用gcc进行编译,因为它仅使用C函数,例如strcpy_s。
而且我有一个C++代码,其中包含一些仅C++库。
是否可以使用gcc和我的代码使用g++一起编译共享对象?
最佳答案
当然,您可以将您的C++程序与共享的C库链接起来。只需通过在C库的头文件中的函数周围添加extern "C" { ... }
,就可以告诉C++编译器该库中的函数具有C链接:
shared_c_lib.h
#ifdef __cplusplus
extern "C" {
#endif
// all your C functions declarations/prototypes
#ifdef __cplusplus
} // extern "C"
#endif