才明白compiler的Og不同与O0

这些天一直在利用visualgdb,一步步跟踪一些比较复杂的代码。发现断点和单步,总是很意外。

所以,将o2改为Og,但发现,现象有所好转,但依然存在。

问了Chatgpt才明白了大概:
You:

I have a cmake file , I compare the different -Og and remove the -Og,
Seems that I can trach into some code no Og , and with Og do not .
I wander Og ’ means?


ChatGPT:

In the context of CMake and compiler flags, -Og refers to a compiler optimization level. It is one of the optimization levels provided by most C and C++ compilers, including GCC (GNU Compiler Collection) and Clang.

The -Og optimization level stands for “optimize for debugging” or “optimize for development.” It includes a set of compiler optimizations that aim to improve debugging capabilities while keeping the generated code reasonably optimized.

When you compile your code with the -Og flag, the compiler performs certain optimizations, such as basic code transformations and elimination of unnecessary variables, but it still tries to preserve the structure and behavior of the code as much as possible. The purpose is to make the generated code easier to debug and understand, with minimal impact on performance.

By removing the -Og flag and not specifying any other optimization level, you will typically use the default optimization level provided by the compiler, which is often -O0. The -O0 level disables almost all optimizations, resulting in code that closely matches the original source code structure but can be significantly slower compared to optimized builds.

If you encounter differences in the behavior of your code between the -Og and non--Og builds, it suggests that some optimizations performed at higher optimization levels are affecting the code execution or potentially hiding certain issues.


You:

Ok ,Thanks , That’s confuse me several days. thanks.


ChatGPT:

You’re welcome! I’m glad I could help clarify the confusion for you. Compiler optimization levels can be a bit tricky to understand, but they play a crucial role in balancing code performance and debugging capabilities. If you have any more questions, feel free to ask. Happy coding!


You:

I want to learn more. Generally , How many compiler optimization level , does gcc or common compilers of C/C++ contains?


ChatGPT:

GCC and many other common C/C++ compilers provide several optimization levels that you can choose from when compiling your code. The number and specific names of these optimization levels may vary slightly between different compilers, but there are typically several common levels. Here are the commonly encountered optimization levels in GCC:

  1. -O0 (default level): This level turns off almost all optimizations. It generates unoptimized code, making it easier to debug but resulting in slower execution.

  2. -O1: This level enables a set of basic optimizations that aim to improve code execution speed without increasing the code size significantly. It includes optimizations such as instruction scheduling, common subexpression elimination, and dead code elimination.

  3. -O2: This level provides more aggressive optimizations compared to -O1. It includes additional transformations like loop unrolling and function inlining. The generated code is faster, but it might be larger.

  4. -O3: This level enables even more aggressive optimizations compared to -O2. It performs further transformations, including vectorization and loop optimizations. The code size might increase significantly, but the execution speed is typically improved.

  5. -Os: This level focuses on optimizing the code size rather than execution speed. It performs a range of size-reducing optimizations, such as function inlining and constant folding.

  6. -Og: This level is specifically optimized for debugging, as explained in the previous response. It provides some basic optimizations to aid debugging while attempting to preserve the original code structure and behavior.

  7. -Ofast: This level enables aggressive optimizations, sacrificing some strict standard compliance for performance gains. It can result in faster code execution but might not adhere strictly to the C/C++ language standards.

These are some of the commonly used optimization levels in GCC and other popular C/C++ compilers. It’s worth noting that the exact behavior of these optimization levels can vary between compilers, so it’s always a good idea to consult the documentation of the specific compiler you’re using for more detailed information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值