KEIL5中C/C++优化等级问题
2025-08-28 来源:cnblogs
C/C++的优化等级会对程序产生 不定性的影响,至于选择哪种优化等级必须从 现有的程序分析才行
可参考知乎上的讲解

原文
0 Minimum optimization. Turns off most optimizations.It gives the best possible debug view and the lowest level of optimization.
近乎不优化,用于调试代码。出现代码行不能设置断点可如此设置试试。
1 Restrictedoptimization. Removes unused inline functions and unused static functions.Turns off optimizations that seriously degrade the debug view. Ifused with --debug, this option gives a satisfactorydebug view with good code density.
部分优化。移除未调用的内联函数和静态函数,关闭debug窗口优化,此状态也能用于调试
2 Highoptimization. If used with --debug, the debug viewmight be less satisfactory because the mapping of object code tosource code is not always clear.
This is the default optimization level.
默认优化等级。如果处于debug状态,部分代码行将不能被调试,具体做了什么优化好像没说
3 Maximumoptimization. -O3 performs the same optimizationsas -O2 however the balance between space and timeoptimizations in the generated code is more heavily weighted towardsspace or time compared with -O2. That is:
-O3 -Otime aims to produce fastercode than -O2 -Otime, at the risk of increasingyour image size
-O3 -Ospace aims to produce smallercode than -O2 -Ospace, but performance might bedegraded.
In addition, -O3 performs extra optimizationsthat are more aggressive, such as:
High-levelscalar optimizations, including loop unrolling, for -O3 -Otime. Thiscan give significant performance benefits at a small code size cost,but at the risk of a longer build time.
More aggressive inlining and automatic inliningfor -O3 -Otime.
-O0
最少的优化,可以最大程度上配合产生代码调试信息,可以在任何代码行打断点,特别是死代码处。
-O1
有限的优化,去除无用的inline和无用的static函数、死代码消除等,在影响到调试信息的地方均不进行优化。在适当的代码体积和充分的调试之间平衡,代码编写阶段最常用的优化等级。
-O2
高度优化,调试信息不友好,有可能会修改代码和函数调用执行流程,自动对函数进行内联等。
-O3
最大程度优化,产生极少量的调试信息。会进行更多代码优化,例如循环展开,更激进的函数内联等。
另外,可以通过单独设置 --loop_optimization_level=option 来控制循环展开的优化等级。
- Keil5安装+STM32CubeMX安装+VSCode辅助开发教程
- 新手入门proteus与keil5软件仿真stm32的手把手超简单教程
- 【51单片机】矩阵键盘逐行扫描法〈C语言+Keil5+Proteus仿真〉
- Keil5 MDK中STM32芯片包下载方法详解
- 【F1-IDE】KEIL5如何打开KEIL4工程
- keil5出现 Error: L6406E: No space in execution regions with .ANY selector matching queue.o(.bss). 的原因总结
- Keil5 STM32 C++开发 ARM V6编译器的使用教程
- freeRTOS V10.0.1移植到STM32F407标准库 - 环境Keil5
- KEIL5 仿真 —— 示波器显示
- 阿里达摩院发布RISC-V CPU玄铁C950,首次原生支持千亿参数大模型
- 六大全新产品系列推出,MCX A微控制器家族迎来创新
- 意法半导体全新STM32C5系列,重新定义入门级微控制器性能与价值,赋能万千智能设备
- 模组复用与整机重测在SRRC、CCC、CTA/NAL认证中的实践操作指南
- 有源晶振与无源晶振的六大区别详解
- 从控制到系统:TI利用边缘AI重塑嵌入式MCU的边界
- 英飞凌持续巩固全球微控制器市场领导地位
- 使用 Keil Studio for Visual Studio Code开发 STM32 设备
- 蓝牙信道探测技术原理与开发套件实践
- Microchip 推出生产就绪型全栈边缘 AI 解决方案,赋能MCU和MPU实现 智能实时决策
- LoRa、LoRaWAN、NB-IoT与4G DTU技术对比及工业无线方案选型分析




