是否可以为单个函数或编译单元使用 O2
/O3
覆盖 -Og
GCC 优化级别?
如果 -Og
用于命令行。
例如,this function doing saturating addition在 ARM Cortex-M4 上。只有当我使用 -O2
编译时,它才会编译为 ssat
,但最右边的编译器使用 -Og
并忽略 pragma。
另一方面,使用较低优化级别的 pragma 是可行的,即指定 O0
作为 pragma 在命令行中覆盖 -Og
。
这是预期的行为吗?
最佳答案
Is it possible to override the -Og GCC optimization level with O2/O3 for a single function or compilation unit?
是也不是。 The documentation for the optimize
attribute ,pragma 文档所指的是:
The
optimize
attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line.
因此属性或编译指示确实覆盖了命令行。然而,这适用于每个功能级别,而 the -Og
option has both per-function and global effects :
Like
-O0
,-Og
completely disables a number of optimization passes so that individual options controlling them have no effect.
因此,虽然使用属性或 pragma 设置优化级别 O2
或 O3
确实会打开该级别中包含的所有单独优化,但这样做可能不会具有在命令行上设置它们的全部效果。
另请注意文档的警告
The
optimize
attribute should be used for debugging purposes only. It is not suitable in production code.
我认为您正在调试,因为这是 -Og
的目的,但是您应该从该评论中了解到的是您不应该依赖用于强制打开或关闭优化选项的技术。正如您所发现的,它可能无法有效或充分地做到这一点。
Is this expected behavior?
您描述的行为似乎与文档一致,尽管它们没有指定细节。然而,您描述它的方式是,当在命令行上设置 -Og
时编译指示被忽略——这不是预期的,我认为不准确。
关于c - GCC pragma 优化不覆盖 `-Og` 命令行设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879091/