增加优化标志时二进制文件怎么可能不改变?

How it's possible a binary is not changed when increasing optimization flag?

我为不同的优化标志编译了相同的程序:-O0-O1-O2-O3。我用过 gcc 和 icc。您可以在下面看到 make 文件的片段:

build-gcc-O3: CXX = g++
build-gcc-O3: BIN_POST_NAME = -gcc-O3
build-gcc-O3: OPT_FLAGS = -O3
build-gcc-O3: fluidsGL

build-icc-O0: CXX = $(INTEL_ICPC)
build-icc-O0: BIN_POST_NAME = -icc-O0
build-icc-O0: OPT_FLAGS = -O0
build-icc-O0: fluidsGL

fluidsGL: fluidsGL.o fluidsGL_cpu.o bilinear_interpolation.o defines.o
   $(CXX) $(CXXFLAGS) $(BINARY_DIR)/defines.o $(BINARY_DIR)/bilinear_interpolation.o $(BINARY_DIR)/fluidsGL_cpu.o $(BINARY_DIR)/fluidsGL.o -o $(BINARY_DIR)/$@$(BIN_POST_NAME)$(DBG_NAME_APPEND) $(OPT_FLAGS) -lGL -lGLU -lGLEW -lglut -lfftw3f

运行 make 后,输出看起来不错。例如,这是 icc 的 make 结果命令 -O3:

"/opt/intel/compilers_and_libraries/linux/bin/intel64/icpc" -Wall bin/defines.o bin/bilinear_interpolation.o bin/fluidsGL_cpu.o bin/fluidsGL.o -o bin/fluidsGL-icc-O3 -O3 -lGL -lGLU -lGLEW -lglut -lfftw3f

编译后,fluidsGL-gcc-O0 和 fluidsGL-gcc-O3(以及 fluidsGL-icc-O0 与 fluidsGL-icc-O3)二进制文件需要相同的磁盘 space,这对我来说已经很奇怪了:

$ ls -la bin/ 
total 728
drwxrwxr-x 3 jesus jesus  4096 ene 18 09:56 .
drwxrwxr-x 7 jesus jesus  4096 ene 18 09:31 ..
-rw-rw-r-- 1 jesus jesus  4200 ene 18 09:56 bilinear_interpolation.o
-rw-rw-r-- 1 jesus jesus  5608 ene 18 09:56 defines.o
-rw-rw-r-- 1 jesus jesus 14952 ene 18 09:56 fluidsGL_cpu.o
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O0
-rwxrwxr-x 1 jesus jesus 43940 ene 18 09:56 fluidsGL-gcc-O0-dbg
-rwxrwxr-x 1 jesus jesus 44272 ene 18 09:56 fluidsGL-gcc-O0-dbg-gprof
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O1
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O2
-rwxrwxr-x 1 jesus jesus 30780 ene 18 09:56 fluidsGL-gcc-O3
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O0
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O1
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O2
-rwxrwxr-x 1 jesus jesus 71151 ene 18 09:56 fluidsGL-icc-O3
-rw-rw-r-- 1 jesus jesus 34664 ene 18 09:56 fluidsGL.o

但即使在使用 diff 比较文件后也没有显示输出,这意味着二进制文件是完全相同的二进制文件:

$ diff bin/fluidsGL-gcc-O0 bin/fluidsGL-gcc-O3
$ diff bin/fluidsGL-icc-O0 bin/fluidsGL-icc-O3
$

这是编译器的正常行为还是我犯了构建错误?

-Ox 标志(其中 x 是优化级别)用于编译器优化,但您实际上不是 re-compiling。您只是 re-linking 已编译的对象 (.o) 文件。

确保将 -Ox 标志添加到目标文件的编译命令中,并进行完全干净的重建。

这很可能是构建错误....或者您希望 makefile 生成重建,尽管没有将其设置为这样做。

目标文件通常可以链接在一起,即使使用不同的优化设置构建也是如此。因此 make 通常不会仅仅因为您指定了优化设置更改就删除目标文件或重建它们。

如果您观察构建的进度,您可能会看到第二个构建没有进行任何重新编译或重新链接。

在 makefile 中设置类似于 clean 目标的东西,它会删除已编译的对象 (*.o) 和可执行文件。并在您要比较的两个构建之间执行 make clean(当然,在确保保留可执行文件之后,否则您无法比较它们)。

您的代码可能非常简单,无法从更改优化设置中获益。但我对此表示怀疑。