可以,CentOS 完全支持 C++11,但是否“开箱即用”取决于你使用的 CentOS 版本 和 GCC 版本。
g++ -std=c++11 test.cpp -o test
⚠️ 注意:
- GCC 4.8 不完全支持 C++14/C++17
- 某些新特性(如
std::make_unique)在 GCC 4.8 中不可用
g++ -std=c++11 test.cpp -o test
或g++ -std=c++17 test.cpp -o test
CentOS 7 默认 GCC 较旧,如果你需要 C++14 / C++17 / C++20,可以使用 Software Collections (SCL) 或 devtoolset。
yum install centos-release-scl
yum install devtoolset-9
scl enable devtoolset-9 bash
g++ --version
然后:
g++ -std=c++17 test.cpp -o test
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
| CentOS 版本 | 默认 GCC | C++11 | 建议 |
|---|---|---|---|
| CentOS 6 | 4.4 | ❌ | 升级系统或使用 SCL |
| CentOS 7 | 4.8.5 | ✅ | 推荐 devtoolset |
| CentOS 8+ | 8/9+ | ✅✅ | 直接使用 |
如果你愿意,可以告诉我:
我可以给你更精准的编译建议。