我正在尝试使用 g++ 编译和链接我的项目。我的项目中的源文件是用 c++ 编写的,但我试图链接的库是用 c 编写的,并且它在头文件中的函数之前没有 extern 来定义它们是用 c 语言编写的。
这是我的编译和链接命令:
g++ socketd.cpp main.cpp -I src/common -L src/common -lwrappers -lerror -lpthread
我收到以下错误:
socketd.cpp:(.text+0xc6): 对 `error_sys(char const*, ...)' 的 undefined reference
并且 error_sys 已经在 liberror 库中定义。
所以我的问题是,为什么会出现此错误?是因为我必须重写函数并在它们之前为用c语言编写的库定义extern吗?
非常感谢。
最佳答案
用 C 编写的库不太可能在其函数周围使用 extern "C"
保护,但这对 C++ 代码来说不是问题。只需自己围绕包含提供它:
extern "C" {
#include <my_c_header.h>
}
关于c++ - g++ 链接器抛出对 xxxx 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578062/