我已经编制了不同的优化参数相同的程序:-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
编译后,fluidGL-gcc-O0和fluidsGL-gcc-O3(以及fluidGL-icc-O0 vs 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
是优化级)是编译器优化,但你没有真正重新编译.您只是重新链接已编译的object(.o
)文件.
确保将-Ox
标志添加到目标文件的编译命令,并执行完全干净的重建.