温馨提示×

centos能否支持c++11

小樊
43
2026-10-08 22:01:54
栏目: 编程语言

可以,CentOS 完全支持 C++11,但是否“开箱即用”取决于你使用的 CentOS 版本 和 GCC 版本。


一、各 CentOS 版本对 C++11 的支持情况

✅ CentOS 7

  • 默认 GCC 版本:4.8.5
  • C++11 支持:✅ 支持(GCC 4.8 已完整支持 C++11)
  • 使用方式:
    g++ -std=c++11 test.cpp -o test
    

⚠️ 注意:

  • GCC 4.8 不完全支持 C++14/C++17
  • 某些新特性(如 std::make_unique)在 GCC 4.8 中不可用

✅ CentOS 8 / Stream

  • 默认 GCC 版本:8.x 或 9.x
  • C++11 / C++14 / C++17:✅ 完整支持
  • 推荐编译方式:
    g++ -std=c++11 test.cpp -o test
    
    或
    g++ -std=c++17 test.cpp -o test
    

二、如果 CentOS 7 想用更高版本 GCC(推荐)

CentOS 7 默认 GCC 较旧,如果你需要 C++14 / C++17 / C++20,可以使用 Software Collections (SCL) 或 devtoolset。

方法 1:使用 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

方法 2:手动编译 GCC(不推荐新手)


三、CMake 中的 C++11 设置示例

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+ ✅✅ 直接使用

如果你愿意,可以告诉我:

  • 你的 CentOS 版本
  • 是否使用 CMake / Makefile
  • 是否需要 C++14 / C++17

我可以给你更精准的编译建议。

0 踩