gcc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGCC
GCC
Purpose
用途
Guide agents through GCC invocation: flag selection, build modes, warning triage, PGO, LTO, and common error patterns. Assume the project uses GNU Make, CMake, or a shell script.
指导Agent完成GCC调用相关操作:标志选择、构建模式、警告分类、PGO、LTO以及常见错误模式。假设项目使用GNU Make、CMake或Shell脚本。
Triggers
触发场景
- "What flags should I use for a release build?"
- "GCC is giving me a warning/error I don't understand"
- "How do I enable LTO / PGO with GCC?"
- "How do I compile with ?"
-fsanitize - "My binary is too large / too slow"
- Undefined reference errors, ABI mismatch, missing symbols
- "我应该为发布构建使用哪些标志?"
- "GCC报了一个我看不懂的警告/错误"
- "如何使用GCC启用LTO/PGO?"
- "如何使用编译?"
-fsanitize - "我的二进制文件太大/太慢"
- 未定义引用错误、ABI不匹配、缺失符号
Workflow
工作流程
1. Choose a build mode
1. 选择构建模式
| Goal | Recommended flags |
|---|---|
| Debug | |
| Debug + debuggable optimisation | |
| Release | |
| Release (max perf, native only) | |
| Release (min size) | |
| Sanitizer (dev) | |
Always pass / (or the required standard) explicitly. Never rely on the implicit default.
-std=c11-std=c++17| 目标 | 推荐标志 |
|---|---|
| 调试 | |
| 调试+可调试优化 | |
| 发布 | |
| 发布(极致性能,仅原生架构) | |
| 发布(最小体积) | |
| Sanitizer(开发环境) | |
始终显式传递 / (或所需的标准)。永远不要依赖隐式默认值。
-std=c11-std=c++172. Warning discipline
2. 警告规范
Start with . For stricter standards compliance add . To treat all warnings as errors in CI: .
-Wall -Wextra-Wpedantic-WerrorSuppress a specific warning only in a narrow scope:
c
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
// ...
#pragma GCC diagnostic popDo not pass (silences everything) except as a last resort for third-party headers.
-w从开始。若要更严格的标准合规性,添加。在CI环境中要将所有警告视为错误:。
-Wall -Wextra-Wpedantic-Werror仅在窄范围内抑制特定警告:
c
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
// ...
#pragma GCC diagnostic pop除非是处理第三方头文件的最后手段,否则不要传递(会屏蔽所有内容)。
-w3. Debug information
3. 调试信息
- — DWARF debug info, default level 2
-g - — includes macro definitions (useful with GDB
-g3)macro expand - — DWARF extensions optimal for GDB
-ggdb - — splits
-gsplit-dwarffiles; reduces link time, needed for.dwodebuginfod
Pair with (not ) when you need readable optimised code in GDB.
-g-Og-O0- — DWARF调试信息,默认级别2
-g - — 包含宏定义(与GDB的
-g3配合使用很有用)macro expand - — 针对GDB优化的DWARF扩展
-ggdb - — 拆分
-gsplit-dwarf文件;减少链接时间,是.dwo所需的debuginfod
当你需要在GDB中查看可读的优化代码时,将与(而非)配合使用。
-g-Og-O04. Optimisation decision tree
4. 优化决策树
text
Need max throughput on a fixed machine?
yes -> -O3 -march=native -flto
no -> profiling available?
yes -> -O2 -fprofile-use
no -> -O2
Size-constrained (embedded, shared lib)?
yes -> -Os (or -Oz with clang)-O3-O2-O3-funswitch-loops-fpeel-loops-floop-interchange-O3-Ofast-ffast-mathtext
需要在固定机器上实现最大吞吐量?
是 -> -O3 -march=native -flto
否 -> 有剖面数据可用?
是 -> -O2 -fprofile-use
否 -> -O2
受体积限制(嵌入式、共享库)?
是 -> -Os (或使用Clang的-Oz)-O3-O2-O3-funswitch-loops-fpeel-loops-floop-interchange-O3-Ofast-ffast-math5. Link-time optimisation (LTO)
5. 链接时优化(LTO)
bash
undefinedbash
undefinedCompile
编译
gcc -O2 -flto -c foo.c -o foo.o
gcc -O2 -flto -c bar.c -o bar.o
gcc -O2 -flto -c foo.c -o foo.o
gcc -O2 -flto -c bar.c -o bar.o
Link (must pass -flto again)
链接(必须再次传递-flto)
gcc -O2 -flto foo.o bar.o -o prog
Use `gcc-ar` / `gcc-ranlib` instead of `ar` / `ranlib` when archiving LTO objects into static libs.
For parallel LTO: `-flto=auto` (uses `make`-style jobserver) or `-flto=N`.
See [references/flags.md](references/flags.md) for full flag reference. See `skills/binaries/linkers-lto` for linker-level LTO configuration.gcc -O2 -flto foo.o bar.o -o prog
将LTO对象归档到静态库时,使用`gcc-ar` / `gcc-ranlib`而非`ar` / `ranlib`。
对于并行LTO:`-flto=auto`(使用`make`风格的任务服务器)或`-flto=N`。
有关完整标志参考,请参见[references/flags.md](references/flags.md)。有关链接器级别的LTO配置,请参见`skills/binaries/linkers-lto`。6. Profile-guided optimisation (PGO)
6. 基于剖面的优化(PGO)
bash
undefinedbash
undefinedStep 1: instrument
步骤1:插装
gcc -O2 -fprofile-generate prog.c -o prog_inst
gcc -O2 -fprofile-generate prog.c -o prog_inst
Step 2: run with representative workload
步骤2:使用代表性工作负载运行
./prog_inst < workload.input
./prog_inst < workload.input
Step 3: optimise with profile
步骤3:使用剖面数据优化
gcc -O2 -fprofile-use -fprofile-correction prog.c -o prog
`-fprofile-correction` handles profile data inconsistencies from multi-threaded runs.gcc -O2 -fprofile-use -fprofile-correction prog.c -o prog
`-fprofile-correction`处理多线程运行时剖面数据的不一致性。7. Preprocessor and standards
7. 预处理器与标准
- Inspect macro expansion:
gcc -E file.c | less - Dump predefined macros:
gcc -dM -E - < /dev/null - Force strict standard:
-std=c11 -pedantic-errors - Disable GNU extensions: (not
-std=c11)-std=gnu11
- 检查宏展开:
gcc -E file.c | less - 转储预定义宏:
gcc -dM -E - < /dev/null - 强制严格标准:
-std=c11 -pedantic-errors - 禁用GNU扩展:(而非
-std=c11)-std=gnu11
8. Common error triage
8. 常见错误分类
| Symptom | Likely cause | Fix |
|---|---|---|
| Missing | Add |
| Variable defined (not just declared) in a header | Add |
| Missing | Add the header |
| Wrong cast or missing prototype | Fix the type; check headers |
| ABI errors with C++ | Mixed | Unify |
| Overflow on a 32-bit relative relocation | Use |
For sanitizer reports, use .
skills/runtimes/sanitizers| 症状 | 可能原因 | 修复方法 |
|---|---|---|
| 缺少 | 添加 |
| 变量在头文件中定义(而非仅声明) | 在头文件中添加 |
| 缺少 | 添加对应的头文件 |
| 错误的类型转换或缺失原型 | 修正类型;检查头文件 |
| C++的ABI错误 | 混合使用不同的 | 统一所有编译单元的 |
| 32位相对重定位溢出 | 使用 |
有关Sanitizer报告的处理,请使用。
skills/runtimes/sanitizers9. Useful one-liners
9. 实用单行命令
bash
undefinedbash
undefinedShow all flags enabled at -O2
显示-O2级别下启用的所有标志
gcc -Q --help=optimizers -O2 | grep enabled
gcc -Q --help=optimizers -O2 | grep enabled
Preprocess only (check includes/macros)
仅预处理(检查头文件/宏)
gcc -E -dD src.c -o src.i
gcc -E -dD src.c -o src.i
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
Show include search path
显示头文件搜索路径
gcc -v -E - < /dev/null 2>&1 | grep -A20 '#include <...>'
gcc -v -E - < /dev/null 2>&1 | grep -A20 '#include <...>'
Check if a flag is supported
检查是否支持某个标志
gcc -Q --help=target | grep march
For a complete flag cheatsheet, see [references/flags.md](references/flags.md).
For common error patterns and examples, see [references/examples.md](references/examples.md).gcc -Q --help=target | grep march
有关完整标志速查表,请参见[references/flags.md](references/flags.md)。
有关常见错误模式和示例,请参见[references/examples.md](references/examples.md)。Related skills
相关技能
- Use to add
skills/runtimes/sanitizersbuilds-fsanitize=* - Use when switching to clang/LLVM
skills/compilers/clang - Use for advanced LTO linker flags
skills/binaries/linkers-lto - Use for debugging GCC-built binaries
skills/debuggers/gdb
- 使用添加
skills/runtimes/sanitizers构建-fsanitize=* - 切换到Clang/LLVM时使用
skills/compilers/clang - 高级LTO链接器标志请使用
skills/binaries/linkers-lto - 调试GCC构建的二进制文件请使用
skills/debuggers/gdb