c-cpp-compilers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseC/C++ Compilers
C/C++ 编译器
Guidance for compiling, analyzing, and optimizing C and C++ code with GCC and Clang in 2026.
2026年使用GCC和Clang编译、分析及优化C和C++代码的指南。
Reference files
参考文档
- GCC specifics: gcc.md -- flags, diagnostics, PGO, LTO, error triage
- Clang specifics: clang.md -- diagnostics, optimization remarks, clang-tidy, macOS
- Sanitizers: sanitizers.md -- ASan, UBSan, TSan, MSan, LSan decision tree and reports
- Static analysis: static-analysis.md -- clang-tidy, cppcheck, scan-build, CI integration
- Modern C/C++: modern-cpp.md -- C++20 modules, C++23/26 features, C23, migration
- GCC 专属内容:gcc.md -- 标志、诊断信息、PGO、LTO、错误排查
- Clang 专属内容:clang.md -- 诊断信息、优化备注、clang-tidy、macOS 适配
- Sanitizer:sanitizers.md -- ASan、UBSan、TSan、MSan、LSan 决策树及报告解读
- 静态分析:static-analysis.md -- clang-tidy、cppcheck、scan-build、CI 集成
- 现代C/C++:modern-cpp.md -- C++20 模块、C++23/26 特性、C23、版本迁移
Standards baseline (2026)
标准基线(2026)
| Language | Preferred standard | GCC support | Clang support |
|---|---|---|---|
| C | | GCC 15+ | Clang 18+ |
| C++ | | GCC 14+ | Clang 18+ |
Always pass the standard flag explicitly. Never rely on compiler defaults.
| 语言 | 推荐标准 | GCC支持版本 | Clang支持版本 |
|---|---|---|---|
| C | | GCC 15+ | Clang 18+ |
| C++ | | GCC 14+ | Clang 18+ |
始终显式传递标准标志,切勿依赖编译器默认值。
Build modes
构建模式
| Goal | Flags |
|---|---|
| Debug | |
| Debug (GDB-friendly optimized) | |
| Release | |
| Release (max throughput, native) | |
| Release (min binary size) | |
| Sanitizer build | |
| 目标 | 标志 |
|---|---|
| 调试 | |
| 调试(GDB友好的优化版本) | |
| 发布 | |
| 发布(最大吞吐量,原生适配) | |
| 发布(最小二进制体积) | |
| Sanitizer 构建 | |
Warning discipline
警告规范
Start with . Add in CI.
-Wall -Wextra -Wpedantic-WerrorSuppress narrow scopes only:
c
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void callback(int ctx, int unused) { (void)ctx; }
#pragma GCC diagnostic popClang equivalent works the same. For project-wide suppression, prefer config or in build system, not in source.
.clang-tidy-Wno-<flag>从 开始,在CI环境中添加 。
-Wall -Wextra -Wpedantic-Werror仅在窄范围内抑制警告:
c
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void callback(int ctx, int unused) { (void)ctx; }
#pragma GCC diagnostic popClang 等效语法作用相同。如需项目级别的警告抑制,优先使用 配置或构建系统中的 ,而非在源码中处理。
.clang-tidy-Wno-<flag>Optimization decision tree
优化决策树
Need max throughput on known hardware?
yes -> -O3 -march=native -flto
no -> Have profiling data?
yes -> -O2 -fprofile-use (GCC) / -fprofile-instr-use (Clang)
no -> -O2
Size-constrained (embedded, shared lib)?
yes -> -Os (GCC/Clang) or -Oz (Clang only)
Numerical code that tolerates IEEE relaxation?
yes -> -Ofast (enables -ffast-math; breaks NaN/inf handling)
no -> stay with -O2/-O3-O3-O2-O3是否需要在已知硬件上达到最大吞吐量?
是 -> -O3 -march=native -flto
否 -> 是否有性能分析数据?
是 -> -O2 -fprofile-use (GCC) / -fprofile-instr-use (Clang)
否 -> -O2
是否受体积限制(嵌入式、共享库)?
是 -> -Os (GCC/Clang) 或 -Oz (仅Clang)
数值计算代码是否允许放宽IEEE标准?
是 -> -Ofast(启用-ffast-math;会破坏NaN/inf处理)
否 -> 保持使用-O2/-O3-O3-O2-O3LTO
LTO
bash
undefinedbash
undefinedGCC
GCC
gcc -O2 -flto=auto -c foo.c bar.c
gcc -O2 -flto=auto foo.o bar.o -o prog
gcc -O2 -flto=auto -c foo.c bar.c
gcc -O2 -flto=auto foo.o bar.o -o prog
Clang (ThinLTO preferred for large projects)
Clang(大型项目优先使用ThinLTO)
clang -O2 -flto=thin -fuse-ld=lld -c foo.c bar.c
clang -O2 -flto=thin -fuse-ld=lld foo.o bar.o -o prog
Use `gcc-ar` / `gcc-ranlib` for GCC LTO archives. Clang ThinLTO links 5-10x faster than full LTO with comparable code quality.clang -O2 -flto=thin -fuse-ld=lld -c foo.c bar.c
clang -O2 -flto=thin -fuse-ld=lld foo.o bar.o -o prog
GCC LTO 归档需使用 `gcc-ar` / `gcc-ranlib`。Clang ThinLTO 的链接速度比全量LTO快5-10倍,且代码质量相当。PGO (profile-guided optimization)
PGO(基于性能分析的优化)
GCC:
bash
gcc -O2 -fprofile-generate prog.c -o prog_inst
./prog_inst < workload.input
gcc -O2 -fprofile-use -fprofile-correction prog.c -o progClang (LLVM instrumentation):
bash
clang -O2 -fprofile-instr-generate prog.c -o prog_inst
./prog_inst < workload.input
llvm-profdata merge -output=prog.profdata default.profraw
clang -O2 -fprofile-instr-use=prog.profdata prog.c -o progGCC:
bash
gcc -O2 -fprofile-generate prog.c -o prog_inst
./prog_inst < workload.input
gcc -O2 -fprofile-use -fprofile-correction prog.c -o progClang(LLVM 插桩):
bash
clang -O2 -fprofile-instr-generate prog.c -o prog_inst
./prog_inst < workload.input
llvm-profdata merge -output=prog.profdata default.profraw
clang -O2 -fprofile-instr-use=prog.profdata prog.c -o progSanitizer quick reference
Sanitizer 速查
| Bug class | Sanitizer | Flag |
|---|---|---|
| Heap/stack/global OOB, use-after-free, double-free | ASan | |
| Signed overflow, null deref, bad shift, misaligned access | UBSan | |
| Data races | TSan | |
| Uninitialised reads (Clang only, all-instrumented build) | MSan | |
| Memory leaks | LSan | via ASan ( |
Common combo:
-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g -O1TSan and MSan are mutually exclusive with ASan. See sanitizers.md for report interpretation.
| 缺陷类型 | Sanitizer | 标志 |
|---|---|---|
| 堆/栈/全局内存越界、释放后使用、重复释放 | ASan | |
| 有符号溢出、空指针解引用、非法移位、未对齐访问 | UBSan | |
| 数据竞争 | TSan | |
| 未初始化读取(仅Clang支持,需全插桩构建) | MSan | |
| 内存泄漏 | LSan | 通过ASan启用( |
常用组合:
-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g -O1TSan 和 MSan 与 ASan 互斥。报告解读请参考 sanitizers.md。
Static analysis (quick start)
静态分析(快速入门)
bash
undefinedbash
undefinedGenerate compilation database
生成编译数据库
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
Run clang-tidy (recommended checks)
运行clang-tidy(推荐检查项)
clang-tidy -checks='bugprone-,clang-analyzer-,performance-,modernize-'
-p build src/foo.cpp
-p build src/foo.cpp
clang-tidy -checks='bugprone-,clang-analyzer-,performance-,modernize-'
-p build src/foo.cpp
-p build src/foo.cpp
Run cppcheck
运行cppcheck
cppcheck --enable=warning,performance,portability --error-exitcode=1 src/
See [static-analysis.md](reference/static-analysis.md) for `.clang-tidy` config, CI integration, and suppression patterns.cppcheck --enable=warning,performance,portability --error-exitcode=1 src/
`.clang-tidy` 配置、CI集成及警告抑制模式请参考 [static-analysis.md](reference/static-analysis.md)。Common error triage
常见错误排查
| Symptom | Likely cause | Fix |
|---|---|---|
| Missing | Libraries after objects: |
| Defined in header without | |
| Missing | Add the header; check |
| Wrong cast or missing prototype | Fix type; enable |
| ABI errors in C++ | Mixed | Unify standard across all TUs |
| 32-bit relocation overflow | |
| 症状 | 可能原因 | 修复方案 |
|---|---|---|
| 缺少 | 库放在目标文件之后: |
| 在头文件中定义变量但未加 | 头文件中用 |
| 缺少 | 添加对应头文件;检查 |
| 类型转换错误或缺少函数原型 | 修正类型;启用 |
| C++ 中的ABI错误 | 混合使用不同 | 统一所有编译单元的标准版本 |
| 32位重定位溢出 | 使用 |
CMake integration
CMake 集成
cmake
cmake_minimum_required(VERSION 3.28)
project(myproject LANGUAGES C CXX)
set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(SANITIZE "Build with ASan+UBSan" OFF)
if(SANITIZE)
set(san_flags -fsanitize=address,undefined -fno-sanitize-recover=all
-fno-omit-frame-pointer -g -O1)
add_compile_options(${san_flags})
add_link_options(${san_flags})
endif()cmake
cmake_minimum_required(VERSION 3.28)
project(myproject LANGUAGES C CXX)
set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(SANITIZE "Build with ASan+UBSan" OFF)
if(SANITIZE)
set(san_flags -fsanitize=address,undefined -fno-sanitize-recover=all
-fno-omit-frame-pointer -g -O1)
add_compile_options(${san_flags})
add_link_options(${san_flags})
endif()Useful one-liners
实用单行命令
bash
undefinedbash
undefinedShow all optimizations enabled at -O2 (GCC)
查看-O2级别下启用的所有优化项(GCC)
gcc -Q --help=optimizers -O2 | grep enabled
gcc -Q --help=optimizers -O2 | grep enabled
Assembly output (Intel syntax)
生成汇编代码(Intel语法)
gcc -S -masm=intel -O2 foo.c -o foo.s
gcc -S -masm=intel -O2 foo.c -o foo.s
Preprocess and dump macros
预处理并导出宏定义
gcc -dM -E - < /dev/null
gcc -dM -E - < /dev/null
Clang optimization remarks (missed vectorization)
Clang 优化备注(未完成向量化)
clang -O2 -Rpass-missed=loop-vectorize src.c
clang -O2 -Rpass-missed=loop-vectorize src.c
Clang save all remarks to YAML
Clang 将所有优化备注保存为YAML
clang -O2 -fsave-optimization-record src.c
clang -O2 -fsave-optimization-record src.c
Show include search path
查看头文件搜索路径
gcc -v -E - < /dev/null 2>&1 | grep -A20 '#include <...>'
undefinedgcc -v -E - < /dev/null 2>&1 | grep -A20 '#include <...>'
undefinedCompiler-specific details
编译器专属细节
For GCC-specific flags, PGO nuances, and error patterns, see gcc.md.
For Clang diagnostics, optimization remarks, macOS toolchain, and clang-tidy, see clang.md.
For C++20 modules, C++23/26, and C23 features, see modern-cpp.md.
GCC专属标志、PGO细节及错误模式请参考 gcc.md。
Clang诊断信息、优化备注、macOS工具链及clang-tidy相关内容请参考 clang.md。
C++20模块、C++23/26及C23特性相关内容请参考 modern-cpp.md。