You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu20.04编译Gromacs时libstdc++.so.6链接错误求助

安装Gromacs时libstdc++.so.6链接错误解决

问题背景

Ubuntu 20.04全新机器(仅使用8小时),CMake配置Gromacs成功,但执行make时出现libstdc++.so.6相关链接错误。

初始错误信息

/usr/bin/ld: CMakeFiles/methane-water-integration.dir/methane-water-integration.cpp.o: undefined reference to symbol '_ZSt20__throw_length_errorPKc@@GLIBCXX_3.4'
/usr/bin/ld: /lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [api/nblib/samples/CMakeFiles/methane-water-integration.dir/build.make:87: bin/methane-water-integration] Error 1
make[1]: *** [CMakeFiles/Makefile2:4790: api/nblib/samples/CMakeFiles/methane-water-integration.dir/all] Error 2
make: *** [Makefile:163: all] Error 2

系统libstdc++.so.6状态

检查/usr/lib/x86_64-linux-gnu和/lib/x86_64-linux-gnu目录,结果如下:

lrwxrwxrwx  2 root root        19 Apr 28  2021 libstdc++.so.6 -> libstdc++.so.6.0.29
-rw-r--r--  1 root root   2186464 Apr 28  2021 libstdc++.so.6.0.29

更新1:make VERBOSE=1详细输出

[ 95%] Linking CXX executable ../../../bin/methane-water-integration
cd /home/anthony/Downloads/gromacs-2022.3/build/api/nblib/samples && /usr/bin/cmake -E cmake_link_script CMakeFiles/methane-water-integration.dir/link.txt --verbose=1
/usr/bin/gcc  -O3 -DNDEBUG   -Wl,-rpath,/home/anthony/anaconda3/lib   CMakeFiles/methane-water-integration.dir/methane-water-integration.cpp.o  -o ../../../bin/methane-water-integration  -Wl,-rpath,"\$ORIGIN/../lib" ../../../lib/libnblib_gmx.so.0.1.0 /usr/lib/gcc/x86_64-linux-gnu/9/libgomp.so /usr/lib/x86_64-linux-gnu/libpthread.so -Wl,-rpath-link,/home/anthony/Downloads/gromacs-2022.3/build/lib 
/usr/bin/ld: CMakeFiles/methane-water-integration.dir/methane-water-integration.cpp.o: undefined reference to symbol '_ZSt20__throw_length_errorPKc@@GLIBCXX_3.4'
/usr/bin/ld: /lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [api/nblib/samples/CMakeFiles/methane-water-integration.dir/build.make:87: bin/methane-water-integration] Error 1
make[2]: Leaving directory '/home/anthony/Downloads/gromacs-2022.3/build'
make[1]: *** [CMakeFiles/Makefile2:4790: api/nblib/samples/CMakeFiles/methane-water-integration.dir/all] Error 2
make[1]: Leaving directory '/home/anthony/Downloads/gromacs-2022.3/build'
make: *** [Makefile:163: all] Error 2

更新2:CMake检测到的编译器信息

-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: /home/anthony/anaconda3/bin/python3.9 (found suitable version "3.9.12", minimum required is "3.7") found components: Interpreter Development 
-- Found OpenMP_C: -fopenmp (found version "4.5") 
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 
-- Found OpenMP: TRUE (found version "4.5")  

解决方案

从make VERBOSE=1输出可见,链接命令使用gcc而非g++,导致未自动链接libstdc++.so,可按以下步骤解决:

  • 重新指定C++编译器配置CMake
    删除现有build目录内容,重新运行CMake时明确指定CXX编译器:

    rm -rf build/*
    cd build
    cmake -DCMAKE_CXX_COMPILER=/usr/bin/g++ ..
    make
    
  • 临时修改样本项目的CMake配置
    在Gromacs源码的api/nblib/samples/CMakeLists.txt中,为目标添加stdc++链接依赖:

    target_link_libraries(methane-water-integration PRIVATE nblib_gmx stdc++)
    

    修改后重新执行make。

  • 排除Anaconda环境干扰
    链接命令中出现Anaconda的rpath,可能导致编译器路径混淆,可在纯净环境下编译:

    conda deactivate
    # 重新执行cmake和make
    
  • 修复系统libstdc++依赖(可选)
    若怀疑系统库损坏,可重装相关依赖:

    sudo apt install --reinstall libstdc++6 gcc-9 g++-9
    

内容的提问来源于stack exchange,提问作者Anthony Nash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 05:10:36