禁用所有GCC优化选项
有关使用GCC编译C程序的默认优化级别-O0。
而根据GCC文档关闭所有优化。
例如:
The default optimization level for compiling C programs using GCC is -O0. which turns off all optimizations according to GCC documentation. for example:
gcc -O0 test.c
不过,要检查是否-O0真的关闭所有的优化即可。我执行这个命令:
However, to check if -O0 is really turning off all optimizations. I executed this command:
gcc -Q -O0 --help=optimizers
在这里,我有点惊讶。我启用了大约50个选项。
然后,我检查通过使用这种与gcc的默认参数:
And here, I was a bit surprised. I got around 50 options enabled. Then, I checked the default arguments passed to gcc using this:
gcc -v
我得到这个:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-
2ubuntu1~14.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --
enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --
program-suffix=-4.8 --enable-shared --enable-linker-build-id --
libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-
gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-
sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-
time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --
with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-
cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-
java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-
jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-
directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-
gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --
with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
所以,我的结论是,我对程序提供的 -O0
标志没有被别的东西overided。
So my conclusion is that the -O0
flag I provided to the program was not overided by something else.
其实,我寻求从头开始实施,其产生的优化选项随机序列的工具,产生的序列比较默认的水平O0-3。就像acovea。原来如此,我想对我产生的序列比较零优化级别(这应该是O0)
In fact, I am seeking to implement from scratch a tool that generates random sequences of optimization options and compare generated sequences to default levels O0-3. Just like "acovea". So that, I would like to compare my generated sequences to a zero-optimization level (which should be O0)
你能解释我为什么50选项默认情况下,在 -O0
?
Can you explain me why 50 options are enabled by default in -O0
?
一个想法我心目中是 O0
+使用关闭默认的优化在 O0
-fno-OPTIMIZATION_NAME 50倍。你觉得呢?
One idea I have in mind is to compile with O0
+ turn off default optimizations in O0
using -fno-OPTIMIZATION_NAME
50 times. What do you think?
好
gcc -O0 `gcc -Q -O0 --help=optimizers 2>&1 | perl -ane 'if ($F[1] =~/enabled/) {$F[0] =~ s/^\s*-f/-fno-/g;push @o,$F[0];}} END {print join(" ", @o)'` your args here
将关闭所有选项关闭(呸)。
will turn all the options off (yuck).
更严重的是,如果你是覆盖所有的优化状态,使优化参数(你需要做的工作)的列表,并明确打开或关闭每一个与 -fmyflag
或 -fno-myflag
。这在本质上回答你的第二个问题。
More seriously, if you are covering all optimisation states, make a list of the optimisation flags (which you need to do anyway), and explicitly turn on or off each one with -fmyflag
or -fno-myflag
. This in essence answers your second question.
您可能,但是,认为它不值得你同时与关闭是对所有的 -O
级别的优化播放。
You may, however, consider it not worth your while playing with turning off optimisations that are on for all -O
levels.
至于为什么的它是这样的,这是介于太宽(即你得去问谁写吧)的,因为那是什么的 https://github.com/gcc-mirror/gcc/blob/master/gcc/toplev。 ç确实的
As to why it's like that, that's somewhere between 'too broad' (i.e. you would have to ask whoever wrote it) and 'because that's what https://github.com/gcc-mirror/gcc/blob/master/gcc/toplev.c does'.
请注意文档不说, -O0
禁用优化。它说(从手册页):
Note the documentation does not say that -O0
disables optimisation. It says (from the man page):
-O0
减少编译时间,使调试产生期望的结果。这是默认的。
-O0
Reduce compilation time and make debugging produce the expected results. This is the default.
这暗示可能存在的优化与不增加编译时间,不影响调试,以及它们将被留在。
By implication there may be optimisations with do not increase compilation time and do not affect debugging, and these will be left on.