入门 Linux 下的 C++ 可以分为「环境准备 → 基础语法 → Linux 特有知识 → 项目实践」几个阶段。下面给你一条比较清晰的路线。
如果你已经有 Linux(Ubuntu / CentOS / Arch 等)可以直接用,否则推荐:
以 Ubuntu 为例:
sudo apt update
sudo apt install build-essential gdb cmake git
检查:
g++ --version
make --version
// hello.cpp
#include <iostream>
int main() {
std::cout << "Hello Linux C++" << std::endl;
return 0;
}
g++ hello.cpp -o hello
./hello
源文件(.cpp) → 预处理 → 编译 → 汇编 → 链接 → 可执行文件
查看过程:
g++ -E hello.cpp # 预处理
g++ -S hello.cpp # 汇编代码
g++ -c hello.cpp # 目标文件
示例:
hello: hello.cpp
g++ hello.cpp -o hello
clean:
rm -f hello
使用:
make
make clean
cmake_minimum_required(VERSION 3.10)
project(hello)
add_executable(hello hello.cpp)
使用:
mkdir build && cd build
cmake ..
make
open / read / write / closefork / execpthreadstd::threadepoll(高性能必备)gdb ./hello
常用:
break mainrunnextprint var日志工具:glog, spdlog
✅ 多动手,少看视频
✅ 每个系统调用都自己写一遍
✅ 学会看 man 手册
✅ 用 Git 管理代码
如果你愿意,我可以:
你现在的基础是?