Loading...
Loading...
Compare original and translation side by side
| Language | Preferred standard | GCC support | Clang support |
|---|---|---|---|
| C | | GCC 15+ | Clang 18+ |
| C++ | | GCC 14+ | Clang 18+ |
| 语言 | 推荐标准 | GCC支持版本 | Clang支持版本 |
|---|---|---|---|
| C | | GCC 15+ | Clang 18+ |
| C++ | | GCC 14+ | Clang 18+ |
| Goal | Flags |
|---|---|
| Debug | |
| Debug (GDB-friendly optimized) | |
| Release | |
| Release (max throughput, native) | |
| Release (min binary size) | |
| Sanitizer build | |
| 目标 | 标志 |
|---|---|
| 调试 | |
| 调试(GDB友好的优化版本) | |
| 发布 | |
| 发布(最大吞吐量,原生适配) | |
| 发布(最小二进制体积) | |
| Sanitizer 构建 | |
-Wall -Wextra -Wpedantic-Werror#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void callback(int ctx, int unused) { (void)ctx; }
#pragma GCC diagnostic pop.clang-tidy-Wno-<flag>-Wall -Wextra -Wpedantic-Werror#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
void callback(int ctx, int unused) { (void)ctx; }
#pragma GCC diagnostic pop.clang-tidy-Wno-<flag>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-O3undefinedundefined
Use `gcc-ar` / `gcc-ranlib` for GCC LTO archives. Clang ThinLTO links 5-10x faster than full LTO with comparable code quality.
GCC LTO 归档需使用 `gcc-ar` / `gcc-ranlib`。Clang ThinLTO 的链接速度比全量LTO快5-10倍,且代码质量相当。gcc -O2 -fprofile-generate prog.c -o prog_inst
./prog_inst < workload.input
gcc -O2 -fprofile-use -fprofile-correction prog.c -o progclang -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 -O2 -fprofile-generate prog.c -o prog_inst
./prog_inst < workload.input
gcc -O2 -fprofile-use -fprofile-correction prog.c -o progclang -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 prog| 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 ( |
-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g -O1| 缺陷类型 | Sanitizer | 标志 |
|---|---|---|
| 堆/栈/全局内存越界、释放后使用、重复释放 | ASan | |
| 有符号溢出、空指针解引用、非法移位、未对齐访问 | UBSan | |
| 数据竞争 | TSan | |
| 未初始化读取(仅Clang支持,需全插桩构建) | MSan | |
| 内存泄漏 | LSan | 通过ASan启用( |
-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g -O1undefinedundefined
See [static-analysis.md](reference/static-analysis.md) for `.clang-tidy` config, CI integration, and suppression patterns.
`.clang-tidy` 配置、CI集成及警告抑制模式请参考 [static-analysis.md](reference/static-analysis.md)。| 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_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_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()undefinedundefinedundefinedundefined