从gcc9.4升级到gcc13.2后,使用c++20编译生成可执行文件运行时报错
错误代码
david@DESKTOP-J901GTO:~$ ./test
./test: /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by ./test)
环境
david@DESKTOP-J901GTO:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/13.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.2.0 (GCC)
编译指令
gcc test.cpp -o test
原因
libstdc++库版本太低,需要升级。一般前面升级gcc时已经安装了最新的libstdc++,只是没有配置系统环境变量,导致程序运行时找到的是旧的库。
解决
- 找到最新的libstdc++库的安装路径
在我的电脑上位置在 /usr/local/lib64 - 将路径加入到 /etc/ld.so.conf.d/x86_64-linux-gnu.conf
然后执行如下命令使路径生效
sudo ldconfig
- 然后再次运行,发现不再报错了。