我为不同的优化标志编译了相同的程序:-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 后,输出看起来不错。例如,这是使用 -O3
为 icc 生成的结果命令:
"/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)二进制文件需要相同的磁盘空间,这对我来说已经很奇怪了:
$ 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
是优化级别)用于编译器优化,但您实际上并不是在重新编译。您只是重新链接已编译的对象 (.o
) 文件。
确保将 -Ox
标志添加到目标文件的编译命令中,并进行完全干净的重建。
关于c++ - 增加优化标志时二进制文件怎么可能不改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41715165/