【发布时间】:2020-02-07 07:19:59
【问题描述】:
我有一个 C 共享对象(.so 文件),我只能使用 gcc 编译它,因为它只使用 C 函数,如 strcpy_s。
我有一个 C++ 代码,其中包含一些仅限 C++ 的库。
可以用gcc编译共享对象,用g++编译我的代码吗?
【问题讨论】:
标签: c++ c gcc compilation g++
我有一个 C 共享对象(.so 文件),我只能使用 gcc 编译它,因为它只使用 C 函数,如 strcpy_s。
我有一个 C++ 代码,其中包含一些仅限 C++ 的库。
可以用gcc编译共享对象,用g++编译我的代码吗?
【问题讨论】:
标签: c++ c gcc compilation 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
【讨论】: