CentOS 下 C++ 报错的情况很多,必须看具体报错信息才能精准解决。不过我可以先给你一个常见报错 + 通用排查思路,你可以对照看看,也可以直接把报错贴出来,我再帮你精确定位。
g++: command not found
或
‘nullptr’ was not declared in this scope
# 查看 gcc 版本
gcc -v
g++ -v
# 安装(CentOS 7)
sudo yum install -y gcc gcc-c++
# 升级到较新版本(推荐 CentOS 7)
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-11
scl enable devtoolset-11 bash
fatal error: iostream: No such file or directory
gcc-c++gcc 编译 C++ 文件sudo yum install -y gcc-c++
g++ main.cpp -o main
error: ‘auto’ not allowed in function return type
g++ -std=c++11 main.cpp
# 或
g++ -std=c++17 main.cpp
./a.out: error while loading shared libraries: libxxx.so.1: cannot open shared object file
# 查找库
ldconfig -p | grep libxxx
# 临时
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
# 永久(推荐)
echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/local.conf
sudo ldconfig
undefined reference to `pthread_create'
g++ main.cpp -lpthread
或
g++ main.cpp -lm -ldl
Cannot find a valid baseurl for repo
cat /etc/centos-release)CentOS 7
g++ 4.8.5
报错:error: ‘to_string’ is not a member of ‘std’编译命令:
g++ main.cpp
我可以直接给你可执行的解决方案 ✅