C++ 开发环境搭建指南
C++ 开发环境搭建指南
面向实际项目开发,精简版。涵盖编译器、IDE、CMake、包管理、调试与代码质量工具。
1. 编译器
| 平台 | 命令 |
|---|---|
| Linux | sudo apt install -y build-essential |
| macOS | xcode-select --install(自带 Clang)或 brew install gcc |
| Windows | 安装 Visual Studio,勾选「使用 C++ 的桌面开发」 |
验证:g++ --version / clang++ --version
2. IDE / 编辑器
推荐组合:VSCode + CMake Tools + clangd
必装 VSCode 扩展:
C/C++(Microsoft)CMake Toolsclangd(代码补全,替代 IntelliSense)CodeLLDB(macOS/Linux 调试)
.vscode/settings.json:
json
{
"cmake.buildDirectory": "${workspaceFolder}/build",
"cmake.generator": "Ninja",
"C_Cpp.default.cppStandard": "c++17",
"editor.formatOnSave": true
}
其他选择:CLion(开箱即用)、Visual Studio(Windows 首选)、Qt Creator(Qt 项目)。
3. CMake 构建系统
安装
bash
# Linux
sudo apt install -y cmake ninja-build
# macOS
brew install cmake ninja
项目结构
project/
├── CMakeLists.txt
├── src/ # 源码
├── include/ # 公共头文件
├── tests/ # 测试
├── build/ # 构建目录(不入库)
├── .clang-format
└── .gitignore
顶层 CMakeLists.txt 模板
cmake
cmake_minimum_required(VERSION 3.20)
project(MyProject VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # 供 clangd 使用
add_compile_options(-Wall -Wextra -Wpedantic)
add_subdirectory(src)
option(BUILD_TESTS "Build tests" ON)
if(BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
常用命令
bash
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug # 配置
cmake --build build -j # 构建
ctest --test-dir build --output-on-failure # 测试
4. 包管理(三选一)
vcpkg(推荐)
bash
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg && ./bootstrap-vcpkg.sh
export VCPKG_ROOT=$(pwd)
cmake -B build -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
项目根目录 vcpkg.json:
json
{
"name": "my-project",
"dependencies": ["fmt", "spdlog", "gtest", "nlohmann-json"]
}
FetchContent(CMake 内置,轻量场景)
cmake
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.2.1
)
FetchContent_MakeAvailable(fmt)
5. 调试工具
| 工具 | 平台 | 用途 |
|---|---|---|
| GDB | Linux | 断点调试(gdb ./app) |
| LLDB | macOS | 断点调试(lldb ./app) |
| AddressSanitizer | 全平台 | 内存错误检测 |
| Valgrind | Linux | 内存泄漏、性能分析 |
启用 AddressSanitizer:
cmake
target_compile_options(myapp PRIVATE -fsanitize=address -fno-omit-frame-pointer)
target_link_options(myapp PRIVATE -fsanitize=address)
常用命令:
bash
valgrind --leak-check=full ./build/bin/myapp # 内存泄漏检测
6. 代码质量
clang-format(格式化)
bash
sudo apt install -y clang-format
clang-format -i src/*.cpp # 格式化
.clang-format:
yaml
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 120
clang-tidy(静态分析)
bash
clang-tidy src/main.cpp -p build # 需要 compile_commands.json
.clang-tidy:
yaml
Checks: 'bugprone-*,performance-*,modernize-*,readability-*'
FormatStyle: file
7. Git 配置
基础配置
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@company.com"
.gitignore(C++ 项目)
gitignore
build/
cmake-build-*/
.vscode/
.idea/
*.o *.a *.so *.dylib *.dll *.exe
vcpkg_installed/
.DS_Store
8. 常见问题
| 问题 | 解决 |
|---|---|
| 链接错误 undefined reference | target_link_libraries(myapp PRIVATE xxx) |
compile_commands.json 不生效 | ln -sf build/compile_commands.json . |
| macOS 上 gcc 实为 Clang | 用 brew install gcc 后使用 gcc-14 |
| 头文件找不到 | target_include_directories(mylib PUBLIC /path) |
💬 评论