在 CentOS 上“检查 C++ 配置是否正确”,通常可以从编译器是否安装、版本是否正常、能否编译运行简单程序、头文件/库路径是否正确几个方面来检查。下面按步骤说明。
which g++
或
g++ --version
如果提示 command not found,说明没有安装。
sudo yum install gcc-c++ make
或(CentOS 8+):
sudo dnf install gcc-c++ make
cat > test.cpp <<EOF
#include <iostream>
int main() {
std::cout << "C++ OK" << std::endl;
return 0;
}
EOF
g++ test.cpp -o test
./test
输出:
C++ OK
说明 C++ 基础配置正确。
g++ -print-search-dirs
echo | g++ -E -x c++ - -v
在输出中可以看到:
#include <...> search starts here:
/usr/include/c++/...
/usr/include
如果这些路径不存在,说明开发包不完整。
g++ -std=c++11 test.cpp -o test11
g++ -std=c++14 test.cpp -o test14
g++ -std=c++17 test.cpp -o test17
如果编译失败,可能是 g++ 版本过低。
g++ -dM -E -x c++ /dev/null | grep __cplusplus
例如使用 pthread:
g++ test.cpp -o test -lpthread
ldd ./test
如果看到 not found,说明库路径有问题。
sudo yum install glibc-headers libstdc++-devel
sudo yum install centos-release-scl
sudo yum install devtoolset-11
scl enable devtoolset-11 bash
g++ --version 正常hello world#include <iostream> 不报错ldd 无 not found如果你是想检查某个项目的 C++ 配置(如 CMake、Makefile、第三方库),可以告诉我具体场景,我可以针对性分析。