用C ++文件编译C共享库

时间:2019-10-09 22:01:28

标签: c++ c gcc compilation g++

我有一个C共享对象(.so文件),我只能使用gcc进行编译,因为它仅使用诸如strcpy_s之类的C函数。

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

是否可以使用gcc和我的代码一起使用gcc编译共享对象?

1 个答案:

答案 0 :(得分:2)

当然,您可以链接您的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