1. 1. C/C++ 开发工具及使用说明
    1. 1.1. 1. 编译器和构建工具
      1. 1.1.1. 1.1 GCC (GNU Compiler Collection)
        1. 1.1.1.1. 安装
        2. 1.1.1.2. 基本使用
      2. 1.1.2. 1.2 Clang/LLVM
        1. 1.1.2.1. 安装
        2. 1.1.2.2. 基本使用
      3. 1.1.3. 1.3 MSVC (Microsoft Visual C++)
        1. 1.1.3.1. 安装
        2. 1.1.3.2. 基本使用
      4. 1.1.4. 1.4 Make
        1. 1.1.4.1. 安装
        2. 1.1.4.2. Makefile 示例
        3. 1.1.4.3. 使用
      5. 1.1.5. 1.5 CMake
        1. 1.1.5.1. 安装
        2. 1.1.5.2. CMakeLists.txt 示例
        3. 1.1.5.3. 使用
      6. 1.1.6. 1.6 Ninja
        1. 1.1.6.1. 安装
        2. 1.1.6.2. 使用
    2. 1.2. 2. 集成开发环境 (IDE)
      1. 1.2.1. 2.1 Visual Studio
        1. 1.2.1.1. 特点
        2. 1.2.1.2. 下载与安装
      2. 1.2.2. 2.2 CLion
        1. 1.2.2.1. 特点
        2. 1.2.2.2. 下载与安装
        3. 1.2.2.3. 基本使用
      3. 1.2.3. 2.3 Eclipse CDT
        1. 1.2.3.1. 安装
        2. 1.2.3.2. 基本使用
      4. 1.2.4. 2.4 Xcode
        1. 1.2.4.1. 安装
        2. 1.2.4.2. 基本使用
    3. 1.3. 3. 代码编辑器
      1. 1.3.1. 3.1 Visual Studio Code
        1. 1.3.1.1. 安装
        2. 1.3.1.2. 必备插件
        3. 1.3.1.3. 基本配置
      2. 1.3.2. 3.2 Vim/Neovim
        1. 1.3.2.1. 必备插件
        2. 1.3.2.2. 配置示例
      3. 1.3.3. 3.3 Sublime Text
        1. 1.3.3.1. 必备插件
    4. 1.4. 4. 调试工具
      1. 1.4.1. 4.1 GDB (GNU Debugger)
        1. 1.4.1.1. 安装
        2. 1.4.1.2. 基本使用
      2. 1.4.2. 4.2 LLDB
        1. 1.4.2.1. 安装
        2. 1.4.2.2. 基本使用
      3. 1.4.3. 4.3 Valgrind
        1. 1.4.3.1. 安装
        2. 1.4.3.2. 基本使用
      4. 1.4.4. 4.4 AddressSanitizer (ASAN)
        1. 1.4.4.1. 使用
      5. 1.4.5. 4.5 UndefinedBehaviorSanitizer (UBSAN)
        1. 1.4.5.1. 使用
    5. 1.5. 5. 版本控制工具
      1. 1.5.1. 5.1 Git
        1. 1.5.1.1. 安装
        2. 1.5.1.2. 基本配置
        3. 1.5.1.3. 基本使用
      2. 1.5.2. 5.2 GitHub/GitLab/Gitee
        1. 1.5.2.1. 基本使用
    6. 1.6. 6. 静态代码分析工具
      1. 1.6.1. 6.1 Clang Static Analyzer
        1. 1.6.1.1. 使用
      2. 1.6.2. 6.2 Cppcheck
        1. 1.6.2.1. 安装
        2. 1.6.2.2. 使用
      3. 1.6.3. 6.3 Clang-Tidy
        1. 1.6.3.1. 安装
        2. 1.6.3.2. 使用
    7. 1.7. 7. 文档生成工具
      1. 1.7.1. 7.1 Doxygen
        1. 1.7.1.1. 安装
        2. 1.7.1.2. 基本使用
        3. 1.7.1.3. 注释格式
      2. 1.7.2. 7.2 Sphinx
        1. 1.7.2.1. 安装
        2. 1.7.2.2. 基本使用
    8. 1.8. 8. 测试框架
      1. 1.8.1. 8.1 Google Test
        1. 1.8.1.1. 安装
        2. 1.8.1.2. 基本使用
      2. 1.8.2. 8.2 CppUnit
        1. 1.8.2.1. 安装
        2. 1.8.2.2. 基本使用
      3. 1.8.3. 8.3 Catch2
        1. 1.8.3.1. 安装
        2. 1.8.3.2. 基本使用
    9. 1.9. 9. 性能分析工具
      1. 1.9.1. 9.1 Valgrind
        1. 1.9.1.1. 使用 Callgrind
        2. 1.9.1.2. 使用 Massif
      2. 1.9.2. 9.2 Perf
        1. 1.9.2.1. 基本使用
      3. 1.9.3. 9.3 Intel VTune Profiler
    10. 1.10. 10. 依赖管理工具
      1. 1.10.1. 10.1 Conan
        1. 1.10.1.1. 安装
        2. 1.10.1.2. 基本使用
      2. 1.10.2. 10.2 vcpkg
        1. 1.10.2.1. 安装
        2. 1.10.2.2. 基本使用
    11. 1.11. 11. 跨平台开发工具
      1. 1.11.1. 11.1 CMake
        1. 1.11.1.1. 跨平台构建
      2. 1.11.2. 11.2 Qt
        1. 1.11.2.1. 安装
        2. 1.11.2.2. 基本使用
    12. 1.12. 12. 命令行工具
      1. 1.12.1. 12.1 Clang Format
        1. 1.12.1.1. 安装
        2. 1.12.1.2. 基本使用
        3. 1.12.1.3. 配置文件 (.clang-format)
      2. 1.12.2. 12.2 Ctags
        1. 1.12.2.1. 安装
        2. 1.12.2.2. 基本使用
      3. 1.12.3. 12.3 Cscope
        1. 1.12.3.1. 安装
        2. 1.12.3.2. 基本使用
    13. 1.13. 13. 常见问题和解决方案
      1. 1.13.1. 13.1 编译错误
      2. 1.13.2. 13.2 运行时错误
      3. 1.13.3. 13.3 性能问题
    14. 1.14. 14. 最佳实践

C/C++ 开发工具及使用说明

C/C++ 开发工具及使用说明

1. 编译器和构建工具

1.1 GCC (GNU Compiler Collection)

GCC 是一套支持多种编程语言的编译器集合,包括 C、C++、Objective-C、Fortran、Ada 等。

安装

1
2
3
4
5
6
7
8
9
# Ubuntu/Debian
sudo apt update
sudo apt install gcc g++

# CentOS/RHEL
sudo yum install gcc gcc-c++

# macOS (使用 Homebrew)
brew install gcc

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 编译单个 C 文件
gcc hello.c -o hello

# 编译单个 C++ 文件
g++ hello.cpp -o hello

# 编译多个源文件
gcc file1.c file2.c -o program
g++ file1.cpp file2.cpp -o program

# 包含头文件路径
gcc -I/path/to/include file.c -o program

# 链接库
gcc file.c -L/path/to/lib -lmylib -o program

# 启用警告信息
gcc -Wall -Wextra file.c -o program

# 优化级别
gcc -O0 file.c -o program # 无优化
gcc -O1 file.c -o program # 基本优化
gcc -O2 file.c -o program # 更多优化
gcc -O3 file.c -o program # 最高优化

# 生成调试信息
gcc -g file.c -o program

1.2 Clang/LLVM

Clang 是一个基于 LLVM 架构的 C/C++/Objective-C 编译器,以其快速的编译速度和详细的错误信息而闻名。

安装

1
2
3
4
5
6
7
8
9
10
11
12
# Ubuntu/Debian
sudo apt update
sudo apt install clang

# CentOS/RHEL
sudo yum install clang

# macOS (已预装)
clang --version

# 使用 Homebrew 更新到最新版本
brew install llvm

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
# 编译 C 文件
clang hello.c -o hello

# 编译 C++ 文件
clang++ hello.cpp -o hello

# 兼容 GCC 命令行选项
clang -Wall -O2 file.c -o program
clang++ -std=c++17 file.cpp -o program

# 使用 Clang 静态分析器
scan-build clang -c file.c

1.3 MSVC (Microsoft Visual C++)

MSVC 是 Microsoft Visual Studio 中的 C/C++ 编译器,主要用于 Windows 平台开发。

安装

从 Microsoft 官网下载并安装 Visual Studio Community、Professional 或 Enterprise 版本。

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
# 在 Developer Command Prompt 中使用
cl hello.c
cl hello.cpp

# 设置输出文件名
cl /Fe:program.exe file.c

# 包含头文件路径
cl /I"C:\path\to\include" file.c

# 链接库
cl file.c /link /LIBPATH:"C:\path\to\lib" mylib.lib

1.4 Make

Make 是一个常用的构建自动化工具,通过 Makefile 定义构建规则。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install make

# CentOS/RHEL
sudo yum install make

# macOS (已预装)
make --version

Makefile 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CC = gcc
CXX = g++
CFLAGS = -Wall -O2
CXXFLAGS = -Wall -O2 -std=c++17
TARGET = program

SRCS = file1.c file2.c main.c
OBJS = $(SRCS:.c=.o)

$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f $(TARGET) $(OBJS)

使用

1
2
3
4
5
6
7
8
9
10
11
# 构建程序
make

# 清理构建文件
make clean

# 强制重新构建
make -B

# 查看执行的命令
make -n

1.5 CMake

CMake 是一个跨平台的构建系统生成器,它可以生成 Makefile、Visual Studio 解决方案等。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install cmake

# CentOS/RHEL
sudo yum install cmake

# macOS (使用 Homebrew)
brew install cmake

CMakeLists.txt 示例

1
2
3
4
5
6
7
8
9
cmake_minimum_required(VERSION 3.10)
project(MyProject C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

add_executable(program main.cpp file1.cpp file2.cpp)
target_include_directories(program PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(program PRIVATE m)

使用

1
2
3
4
5
6
7
8
9
10
11
12
# 创建构建目录
mkdir build
cd build

# 生成构建文件
cmake ..

# 构建程序
make

# 安装程序
sudo make install

1.6 Ninja

Ninja 是一个轻量级的构建系统,专注于快速构建。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install ninja-build

# CentOS/RHEL
sudo yum install ninja-build

# macOS (使用 Homebrew)
brew install ninja

使用

1
2
3
4
5
6
# 与 CMake 配合使用
cmake -G Ninja ..
ninja

# 清理构建文件
ninja clean

2. 集成开发环境 (IDE)

2.1 Visual Studio

Visual Studio 是 Microsoft 开发的强大 IDE,提供完整的 C/C++ 开发环境。

特点

  • 智能代码补全
  • 强大的调试功能
  • 集成的 Git 支持
  • 丰富的插件生态
  • 支持 Windows 和 macOS

下载与安装

从 Microsoft 官网下载 Visual Studio Community、Professional 或 Enterprise 版本。

2.2 CLion

CLion 是 JetBrains 开发的跨平台 C/C++ IDE,基于 IntelliJ 平台。

特点

  • 智能代码补全和导航
  • 强大的静态分析
  • 内置调试器
  • 支持 CMake、Makefile、QMake 等构建系统
  • 集成版本控制

下载与安装

从 JetBrains 官网下载 CLion。

基本使用

  1. 创建新项目:选择 C++ 可执行文件或库
  2. 配置工具链:选择编译器 (GCC/Clang/MSVC) 和调试器
  3. 编写代码:利用智能补全和导航功能
  4. 构建和运行:使用工具栏上的按钮或快捷键
  5. 调试:设置断点,单步执行代码

2.3 Eclipse CDT

Eclipse CDT (C/C++ Development Tooling) 是 Eclipse 平台上的 C/C++ 开发工具插件。

安装

  1. 下载 Eclipse IDE for C/C++ Developers
  2. 解压并运行

基本使用

  1. 创建新项目:选择 C++ 项目
  2. 选择构建系统:Makefile、CMake 等
  3. 编写代码:使用代码补全和导航功能
  4. 构建和运行:使用工具栏上的按钮

2.4 Xcode

Xcode 是 Apple 开发的 macOS 和 iOS 开发 IDE,支持 C、C++、Objective-C 和 Swift。

安装

从 Mac App Store 下载并安装 Xcode。

基本使用

  1. 创建新项目:选择 Command Line Tool
  2. 选择语言:C 或 C++
  3. 编写代码:使用代码补全和导航功能
  4. 构建和运行:使用工具栏上的按钮或快捷键

3. 代码编辑器

3.1 Visual Studio Code

VS Code 是微软开发的轻量级代码编辑器,通过插件支持 C/C++ 开发。

安装

从 VS Code 官网下载并安装。

必备插件

  • C/C++ (Microsoft)
  • C/C++ Extension Pack (Microsoft)
  • CMake Tools
  • CodeLLDB (调试器)

基本配置

创建 .vscode 目录,添加以下配置文件:

c_cpp_properties.json (编译器配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"configurations": [
{
"name": "Linux",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

launch.json (调试配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file"
}
]
}

tasks.json (构建配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}

3.2 Vim/Neovim

Vim 和 Neovim 是强大的文本编辑器,通过插件可以扩展为功能丰富的 C/C++ 开发环境。

必备插件

  • coc.nvim:智能代码补全
  • vim-lsp:语言服务器协议支持
  • ale:异步 linting 和语法检查
  • nerdtree:文件浏览器
  • tagbar:代码结构浏览
  • vim-fugitive:Git 集成

配置示例

~/.vimrc~/.config/nvim/init.vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
" 基本设置
set number
set tabstop=4
set shiftwidth=4
set expandtab
set cursorline
set autoindent

" 插件管理 (使用 vim-plug)
call plug#begin('~/.vim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'dense-analysis/ale'
Plug 'preservim/nerdtree'
Plug 'majutsushi/tagbar'
Plug 'tpope/vim-fugitive'
call plug#end()

" Coc 配置
let g:coc_global_extensions = ['coc-ccls', 'coc-json', 'coc-yaml', 'coc-git']

" ALE 配置
let g:ale_linters = {
\ 'c': ['gcc'],
\ 'cpp': ['g++'],
\}

let g:ale_fixers = {
\ 'c': ['clang-format'],
\ 'cpp': ['clang-format'],
\}

" 快捷键映射
nnoremap <F2> :NERDTreeToggle<CR>
nnoremap <F3> :TagbarToggle<CR>
nnoremap <Leader>g :Gstatus<CR>

3.3 Sublime Text

Sublime Text 是一个轻量级的文本编辑器,通过插件支持 C/C++ 开发。

必备插件

  • Package Control
  • C++ (默认插件)
  • CMake
  • GitGutter
  • SublimeLinter
  • Clang Format

4. 调试工具

4.1 GDB (GNU Debugger)

GDB 是一个强大的命令行调试器,支持多种编程语言。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install gdb

# CentOS/RHEL
sudo yum install gdb

# macOS (使用 Homebrew)
brew install gdb

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 启动 GDB
gdb program

# 设置断点
b file.c:10

# 运行程序
run

# 单步执行
step # 进入函数
next # 不进入函数

# 继续执行
continue

# 查看变量
print variable

# 查看函数调用栈
backtrace

# 退出 GDB
quit

4.2 LLDB

LLDB 是 LLVM 项目的一部分,是一个现代化的调试器,提供与 GDB 类似的功能。

安装

LLDB 通常与 Clang 一起安装。

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 启动 LLDB
lldb program

# 设置断点
b file.cpp:10

# 运行程序
run

# 单步执行
step # 进入函数
next # 不进入函数

# 查看变量
print variable

# 查看函数调用栈
bt

# 退出 LLDB
quit

4.3 Valgrind

Valgrind 是一个内存调试和性能分析工具集,主要用于检测内存泄漏和越界访问。

安装

1
2
3
4
5
# Ubuntu/Debian
sudo apt install valgrind

# CentOS/RHEL
sudo yum install valgrind

基本使用

1
2
3
4
5
6
7
8
# 检测内存泄漏
valgrind --leak-check=full ./program

# 检测内存访问错误
valgrind --tool=memcheck ./program

# 性能分析
valgrind --tool=callgrind ./program

4.4 AddressSanitizer (ASAN)

AddressSanitizer 是 GCC 和 Clang 内置的内存错误检测器。

使用

1
2
3
4
5
6
# 编译时启用 ASAN
gcc -fsanitize=address -g file.c -o program
g++ -fsanitize=address -g file.cpp -o program

# 运行程序
./program

4.5 UndefinedBehaviorSanitizer (UBSAN)

UndefinedBehaviorSanitizer 是 GCC 和 Clang 内置的未定义行为检测器。

使用

1
2
3
4
5
6
# 编译时启用 UBSAN
gcc -fsanitize=undefined -g file.c -o program
g++ -fsanitize=undefined -g file.cpp -o program

# 运行程序
./program

5. 版本控制工具

5.1 Git

Git 是目前最流行的分布式版本控制系统。

安装

1
2
3
4
5
6
7
8
9
10
11
# Ubuntu/Debian
sudo apt install git

# CentOS/RHEL
sudo yum install git

# macOS (已预装)
git --version

# 使用 Homebrew 更新到最新版本
brew install git

基本配置

1
2
3
4
5
6
7
8
9
# 设置用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 设置默认编辑器
git config --global core.editor vim

# 启用彩色输出
git config --global color.ui true

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 初始化仓库
git init

# 克隆仓库
git clone https://github.com/user/repo.git

# 查看状态
git status

# 添加文件到暂存区
git add file.c file.cpp
git add . # 添加所有文件

# 提交更改
git commit -m "Commit message"

# 推送到远程仓库
git push origin main

# 从远程仓库拉取
git pull origin main

# 查看历史记录
git log
git log --oneline # 简洁格式

# 创建分支
git branch feature-branch

# 切换分支
git checkout feature-branch

# 合并分支
git checkout main
git merge feature-branch

# 解决冲突
git status # 查看冲突文件
# 手动编辑冲突文件
# 然后提交更改
git add resolved-file
git commit -m "Resolve conflict"

5.2 GitHub/GitLab/Gitee

这些是基于 Git 的代码托管平台,提供远程仓库托管、代码审查、CI/CD 等功能。

基本使用

  1. 创建远程仓库
  2. 将本地仓库推送到远程
  3. 创建 Pull Request/Merge Request
  4. 审查代码并合并
  5. 设置 CI/CD 流水线

6. 静态代码分析工具

6.1 Clang Static Analyzer

Clang 静态分析器是 Clang 编译器的一部分,用于检测代码中的潜在错误。

使用

1
2
3
4
5
6
# 命令行使用
scan-build clang -c file.c

# 与 CMake 配合使用
scan-build cmake ..
scan-build make

6.2 Cppcheck

Cppcheck 是一个开源的静态代码分析工具,专门用于 C/C++ 代码。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install cppcheck

# CentOS/RHEL
sudo yum install cppcheck

# macOS (使用 Homebrew)
brew install cppcheck

使用

1
2
3
4
5
6
7
8
9
10
11
12
# 分析单个文件
cppcheck file.c

# 分析整个目录
cppcheck src/

# 启用所有检查
cppcheck --enable=all file.c

# 生成 HTML 报告
cppcheck --enable=all --xml --xml-version=2 file.c 2> report.xml
cppcheck-htmlreport --file=report.xml --report-dir=report

6.3 Clang-Tidy

Clang-Tidy 是一个基于 Clang 的 C/C++ 静态分析工具,提供了一系列的检查和修复建议。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install clang-tidy

# CentOS/RHEL
sudo yum install clang-tidy

# macOS (使用 Homebrew)
brew install clang-format llvm

使用

1
2
3
4
5
6
7
8
9
# 分析单个文件
clang-tidy file.cpp -- -std=c++17

# 与 CMake 配合使用
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
clang-tidy file.cpp

# 自动修复问题
clang-tidy file.cpp --fix -- -std=c++17

7. 文档生成工具

7.1 Doxygen

Doxygen 是一个用于生成代码文档的工具,支持多种编程语言,包括 C/C++。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install doxygen graphviz

# CentOS/RHEL
sudo yum install doxygen graphviz

# macOS (使用 Homebrew)
brew install doxygen graphviz

基本使用

  1. 初始化配置文件

    1
    doxygen -g Doxyfile
  2. 编辑 Doxyfile 配置文件

    1
    2
    3
    4
    5
    PROJECT_NAME = "My Project"
    INPUT = src/
    OUTPUT_DIRECTORY = doc/
    GENERATE_HTML = YES
    GENERATE_LATEX = NO
  3. 生成文档

    1
    doxygen Doxyfile

注释格式

1
2
3
4
5
6
7
8
9
10
/**
* @brief 计算两个数的和
*
* @param a 第一个数
* @param b 第二个数
* @return 两个数的和
*/
int add(int a, int b) {
return a + b;
}

7.2 Sphinx

Sphinx 是一个用于生成文档的工具,主要用于 Python 项目,但也可以用于 C/C++ 项目。

安装

1
pip install sphinx breathe sphinx-rtd-theme

基本使用

  1. 初始化项目

    1
    2
    3
    mkdir doc
    cd doc
    sphinx-quickstart
  2. 配置 conf.py

    1
    2
    3
    4
    5
    6
    7
    8
    import os
    import sys
    sys.path.insert(0, os.path.abspath('..'))

    extensions = ['breathe']
    breathe_projects = {'MyProject': '../doxygen/xml'}
    breathe_default_project = 'MyProject'
    html_theme = 'sphinx_rtd_theme'
  3. 生成文档

    1
    2
    doxygen Doxyfile
    make html

8. 测试框架

8.1 Google Test

Google Test 是 Google 开发的 C++ 测试框架。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Ubuntu/Debian
sudo apt install libgtest-dev

# CentOS/RHEL
sudo yum install gtest-devel

# 从源码安装
# 克隆仓库
https://github.com/google/googletest.git

# 构建和安装
cd googletest
mkdir build
cd build
cmake ..
make
sudo make install

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <gtest/gtest.h>

// 测试函数
int add(int a, int b) {
return a + b;
}

// 测试用例
TEST(AddTest, PositiveNumbers) {
EXPECT_EQ(add(1, 2), 3);
EXPECT_EQ(add(10, 20), 30);
}

TEST(AddTest, NegativeNumbers) {
EXPECT_EQ(add(-1, -2), -3);
EXPECT_EQ(add(-10, 20), 10);
}

// 主函数
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
1
2
3
4
5
# 编译测试程序
g++ test.cpp -lgtest -lgtest_main -pthread -o test

# 运行测试
./test

8.2 CppUnit

CppUnit 是一个基于 JUnit 的 C++ 测试框架。

安装

1
2
3
4
5
# Ubuntu/Debian
sudo apt install libcppunit-dev

# CentOS/RHEL
sudo yum install cppunit-devel

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cppunit/TestCase.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TextOutputter.h>

// 测试类
class AddTest : public CppUnit::TestCase {
public:
void testPositiveNumbers() {
CPPUNIT_ASSERT(add(1, 2) == 3);
CPPUNIT_ASSERT(add(10, 20) == 30);
}

void testNegativeNumbers() {
CPPUNIT_ASSERT(add(-1, -2) == -3);
CPPUNIT_ASSERT(add(-10, 20) == 10);
}

// 注册测试方法
CPPUNIT_TEST_SUITE(AddTest);
CPPUNIT_TEST(testPositiveNumbers);
CPPUNIT_TEST(testNegativeNumbers);
CPPUNIT_TEST_SUITE_END();

private:
int add(int a, int b) {
return a + b;
}
};

CPPUNIT_TEST_SUITE_REGISTRATION(AddTest);

// 主函数
int main(int argc, char **argv) {
CppUnit::TestResult result;
CppUnit::TestResultCollector collector;
result.addListener(&collector);

CppUnit::TestRunner runner;
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
runner.run(result);

CppUnit::TextOutputter outputter(&collector, std::cout);
outputter.write();

return collector.wasSuccessful() ? 0 : 1;
}
1
2
3
4
5
# 编译测试程序
g++ test.cpp -lcppunit -o test

# 运行测试
./test

8.3 Catch2

Catch2 是一个现代化的 C++ 测试框架,无需复杂的设置。

安装

1
2
3
4
5
6
7
8
9
10
11
# 从源码安装
# 克隆仓库
https://github.com/catchorg/Catch2.git

# 构建和安装
cd Catch2
mkdir build
cd build
cmake ..
make
sudo make install

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define CATCH_CONFIG_MAIN  // 让 Catch2 生成主函数
#include <catch2/catch.hpp>

// 测试函数
int add(int a, int b) {
return a + b;
}

// 测试用例
TEST_CASE("AddTest") {
SECTION("PositiveNumbers") {
REQUIRE(add(1, 2) == 3);
REQUIRE(add(10, 20) == 30);
}

SECTION("NegativeNumbers") {
REQUIRE(add(-1, -2) == -3);
REQUIRE(add(-10, 20) == 10);
}
}
1
2
3
4
5
# 编译测试程序
g++ test.cpp -o test

# 运行测试
./test

9. 性能分析工具

9.1 Valgrind

Valgrind 不仅可以检测内存错误,还可以进行性能分析。

使用 Callgrind

1
2
3
4
5
# 运行程序并收集性能数据
valgrind --tool=callgrind ./program

# 分析性能数据
kcachegrind callgrind.out.*

使用 Massif

1
2
3
4
5
# 运行程序并收集内存分配数据
valgrind --tool=massif ./program

# 分析内存分配数据
ms_print massif.out.*

9.2 Perf

Perf 是 Linux 内核提供的性能分析工具。

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装
# Ubuntu/Debian
sudo apt install linux-tools-common linux-tools-generic

# CentOS/RHEL
sudo yum install perf

# 运行性能分析
perf record ./program

# 查看性能报告
perf report

# 查看函数调用图
perf report -g

9.3 Intel VTune Profiler

Intel VTune Profiler 是 Intel 开发的性能分析工具,用于识别和优化应用程序性能。

10. 依赖管理工具

10.1 Conan

Conan 是一个 C/C++ 包管理器,支持多种平台和构建系统。

安装

1
pip install conan

基本使用

  1. 创建 conanfile.txt

    1
    2
    3
    4
    5
    [requires]
    boost/1.78.0

    [generators]
    cmake
  2. 安装依赖

    1
    2
    3
    mkdir build
    cd build
    conan install ..
  3. 配置和构建

    1
    2
    cmake ..
    make

10.2 vcpkg

vcpkg 是 Microsoft 开发的 C/C++ 包管理器,支持 Windows、macOS 和 Linux。

安装

1
2
3
4
5
6
7
8
9
# 克隆仓库
git clone https://github.com/Microsoft/vcpkg.git

# 安装
cd vcpkg
./bootstrap-vcpkg.sh # Linux/macOS

# 集成到系统
sudo ./vcpkg integrate install

基本使用

1
2
3
4
5
6
# 安装包
./vcpkg install boost:x64-linux

# 在 CMake 中使用
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build

11. 跨平台开发工具

11.1 CMake

CMake 不仅是构建系统生成器,也是跨平台开发的重要工具。

跨平台构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cmake_minimum_required(VERSION 3.10)
project(MyProject)

# 设置 C/C++ 标准
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# 添加可执行文件
add_executable(program main.cpp)

# 平台特定配置
if(WIN32)
# Windows 特定代码
target_compile_definitions(program PRIVATE WIN32)
elif(UNIX AND NOT APPLE)
# Linux 特定代码
target_compile_definitions(program PRIVATE LINUX)
elif(APPLE)
# macOS 特定代码
target_compile_definitions(program PRIVATE MACOS)
endif()

11.2 Qt

Qt 是一个跨平台的 C++ 应用程序框架,用于开发图形用户界面应用程序。

安装

从 Qt 官网下载 Qt 安装程序。

基本使用

1
2
3
4
5
6
7
8
9
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QLabel label("Hello, Qt!");
label.show();
return app.exec();
}
1
2
3
4
5
6
7
8
9
10
11
cmake_minimum_required(VERSION 3.10)
project(MyQtApp)

# 查找 Qt 包
find_package(Qt5 COMPONENTS Widgets REQUIRED)

# 添加可执行文件
add_executable(myapp main.cpp)

# 链接 Qt 库
target_link_libraries(myapp PRIVATE Qt5::Widgets)

12. 命令行工具

12.1 Clang Format

Clang Format 是一个代码格式化工具,支持多种编程语言。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install clang-format

# CentOS/RHEL
sudo yum install clang-format

# macOS (使用 Homebrew)
brew install clang-format

基本使用

1
2
3
4
5
# 格式化单个文件
clang-format -i file.cpp

# 格式化整个目录
find . -name "*.cpp" -o -name "*.h" | xargs clang-format -i

配置文件 (.clang-format)

1
2
3
4
5
6
---
Language: Cpp
BasedOnStyle: Google
IndentWidth: 4
UseTab: Never
ColumnLimit: 80

12.2 Ctags

Ctags 是一个用于生成标签文件的工具,用于代码导航。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install exuberant-ctags

# CentOS/RHEL
sudo yum install ctags

# macOS (使用 Homebrew)
brew install ctags

基本使用

1
2
3
4
5
6
7
# 生成标签文件
ctags -R .

# 在 Vim 中使用
:tag function_name # 跳转到函数定义
C-] # 跳转到定义
C-t # 返回

12.3 Cscope

Cscope 是一个用于代码浏览和分析的工具。

安装

1
2
3
4
5
6
7
8
# Ubuntu/Debian
sudo apt install cscope

# CentOS/RHEL
sudo yum install cscope

# macOS (使用 Homebrew)
brew install cscope

基本使用

1
2
3
4
5
6
# 生成数据库
cscope -Rbq

# 在 Vim 中使用
:cs find f function_name # 查找函数定义
:cs find c function_name # 查找函数调用

13. 常见问题和解决方案

13.1 编译错误

问题undefined reference to 'function'
解决方案:确保函数已定义,并且链接了包含该函数的库。

问题fatal error: header.h: No such file or directory
解决方案:使用 -I 选项添加头文件路径。

问题error: 'for' loop initial declarations are only allowed in C99 mode
解决方案:使用 -std=c99-std=c11 选项启用 C99 或 C11 标准。

13.2 运行时错误

问题:段错误 (Segmentation fault)
解决方案:使用调试器 (GDB/LLDB) 定位错误,检查空指针、数组越界等问题。

问题:内存泄漏
解决方案:使用 Valgrind 或 AddressSanitizer 检测内存泄漏,确保所有动态分配的内存都已释放。

13.3 性能问题

问题:程序运行缓慢
解决方案:使用性能分析工具 (Valgrind/Perf/VTune) 定位性能瓶颈,优化算法和数据结构。

14. 最佳实践

  1. 代码风格:使用一致的代码风格,如 Google C++ Style Guide 或 LLVM Coding Standards。
  2. 版本控制:使用 Git 进行版本控制,遵循清晰的提交规范。
  3. 单元测试:编写单元测试确保代码质量。
  4. 静态分析:使用静态分析工具检测潜在问题。
  5. 文档:编写清晰的注释和文档。
  6. 依赖管理:使用包管理器管理依赖。
  7. 性能优化:在确保正确性的前提下进行性能优化。
  8. 跨平台:考虑跨平台兼容性。

希望这份 C/C++ 开发工具及使用说明对你有所帮助!如果有任何问题,请随时查阅相关工具的官方文档。