gcc编译执行报错:undefined reference to symbol '_ZNSsD1Ev@@GLIBCXX_3.4'
[root@localhost linux]# gcc main.cpp -L ./ -lcarAnaly -o out
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /tmp/cca7kJoh.o: undefined reference to symbol '_ZNSsD1Ev@@GLIBCXX_3.4'
//lib64/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
这个错误信息表明链接器在尝试解析 main.cpp 编译后生成的 .o 文件时遇到了问题。具体来说,错误是关于 std::string 的析构函数 std::string::~string() 的未定义引用,这通常与 C++ 标准库 libstdc++ 的版本有关。
错误信息中的关键部分是:
undefined reference to symbol '_ZNSsD1Ev@@GLIBCXX_3.4':表示链接器找不到std::string析构函数的定义。/lib64/libstdc++.so.6: error adding symbols: DSO missing from command line:表示链接器在命令行中没有找到动态共享对象(DSO),即libstdc++.so.6。
要解决这个问题,你可以尝试以下方法:
-
确保使用
g++而不是gcc:由于main.cpp是一个 C++ 源文件,应该使用g++来编译和链接:g++ main.cpp -L ./ -lcarAnaly -o out

浙公网安备 33010602011771号