在 CentOS 上学习 C++ 开发,可以从「环境准备 → 基础学习 → 工程实践 → 进阶方向」一步一步来。下面给你一套比较实用、适合新手的路线。
CentOS 默认可能没有 g++,先安装:
sudo yum install -y gcc gcc-c++ make
查看版本:
g++ --version
建议使用 GCC 7 以上,如果想用 C++17/C++20,可以安装
devtoolset(后面说)。
Vim 基础:
sudo yum install -y vim
mkdir cpp_learn && cd cpp_learn
vim hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello CentOS C++" << endl;
return 0;
}
g++ hello.cpp -o hello
./hello
vectorstringmaphello: hello.cpp
g++ hello.cpp -o hello
clean:
rm -f hello
使用:
make
./hello
安装:
sudo yum install -y cmake
示例 CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(hello)
add_executable(hello hello.cpp)
编译:
mkdir build && cd build
cmake ..
make
./hello
CentOS 7 自带 GCC 太老,可用:
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-11
scl enable devtoolset-11 bash
验证:
g++ --version
安装:
sudo yum install -y gdb
编译带调试信息:
g++ -g main.cpp -o main
gdb ./main
常用:
break main
run
next
print x
quit
std::threadpthread✅ 每天写代码
✅ 小项目驱动(计算器、通讯录、聊天室)
✅ 不急着学框架
✅ 多用终端 & 看报错
如果你愿意,我可以:
你现在是 完全新手 还是 有别的语言基础?