链接器GCC对符号的未定义引用

Linker GCC Undefined reference to symbol

本文关键字:未定义 引用 符号 GCC 链接      更新时间:2023-10-16

我正在尝试以下命令:

gcc -o test test.o -lblas

给出错误:

/usr/bin/ld: test.o: undefined reference to symbol 'sqrtf@@GLIBC_2.2.5'
/usr/bin/ld: note: 'sqrtf@@GLIBC_2.2.5' is defined in DSO /lib64/libm.so.6 so try adding     it to the linker command line

/lib64/libm.so.6:无法读取符号:无效操作collect2:错误:ld返回1退出状态

然而,当我在命令末尾添加-lm时,会出现一大堆错误消息:

test.o: In function `main':
test.cpp:(.text+0xe9): undefined reference to `std::cout'
test.cpp:(.text+0xee): undefined reference to `std::basic_ostream<char, 
std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char,   
std::char_traits<char> >&, char const*)'
[...]
collect2: error: ld returned 1 exit status

当我试图链接其他库时,我也遇到了同样的问题。

以下是我的解决方案:

  1. 安装libtool;

  2. 将库路径添加到LD_library_path;

  3. 我用根来做。

我不知道它在我的Ubuntu上工作的确切原因。但我认为你可以试试这些步骤。