C++ 开发环境搭建指南

✍️ Demo User·📅 2026年4月21日·👁 13 次阅读
c++开发
📚 系列:现代 C++ 学习之路

C++ 开发环境搭建指南

面向实际项目开发,精简版。涵盖编译器、IDE、CMake、包管理、调试与代码质量工具。


1. 编译器

平台命令
Linuxsudo apt install -y build-essential
macOSxcode-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 Tools
  • clangd(代码补全,替代 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. 调试工具

工具平台用途
GDBLinux断点调试(gdb ./app
LLDBmacOS断点调试(lldb ./app
AddressSanitizer全平台内存错误检测
ValgrindLinux内存泄漏、性能分析

启用 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 referencetarget_link_libraries(myapp PRIVATE xxx)
compile_commands.json 不生效ln -sf build/compile_commands.json .
macOS 上 gcc 实为 Clangbrew install gcc 后使用 gcc-14
头文件找不到target_include_directories(mylib PUBLIC /path)

💬 评论

加载评论中...