跳到主要内容

编译 Python 3.6:使用共享库还是开启优化

· 阅读需 2 分钟

从 Python 3.6 开始,Python 引入了 gcc 的 Link Time Optimization(lto)和 Profile-guided Optimization(pgo)。但我在 Cent OS 7.2上编译 Python 3.6 时发现似乎共享库与优化不可兼得。为了进一步验证这不是我的问题,我做了多次对比实验,暂时将结果记录于下。

这个问题我已经在 bugs.python.org 上提了 issue 。但似乎没人理我呢。( ´_ゝ`)旦

系统信息

$ uname -a
Linux VM_222_40_centos 3.10.0-327.36.3.el7.x86_64 #1 SMP Mon Oct 24 16:09:20 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)

同时开启共享库与优化

./configure --enable-shared --enable-optimizations --prefix=$HOME/.local LDFLAGS=-Wl,-rpath=$HOME/.local/lib
make

构建时出现错误,最后几行如下:

gcc -pthread -shared -Wl,-rpath=/home/halfcoder/.local/lib -fprofile-generate  -Wl,--no-as-needed -o libpython3.so -Wl,-hlibpython3.so libpython3.6m.so
gcc -pthread -Wl,-rpath=/home/halfcoder/.local/lib -fprofile-generate -Xlinker -export-dynamic -o python Programs/python.o -L. -lpython3.6m -lpthread -ldl -lutil -lm
LD_LIBRARY_PATH=/home/halfcoder/.local/src/Python-3.6.0-optmiz ./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
echo "generate-posix-vars failed" ; \
rm -f ./pybuilddir.txt ; \
exit 1 ; \
fi
./python: symbol lookup error: ./python: undefined symbol: __gcov_indirect_call_profiler
generate-posix-vars failed
make[2]: *** [pybuilddir.txt] Error 1
make[2]: Leaving directory `/home/halfcoder/.local/src/Python-3.6.0-optmiz'
make[1]: *** [build_all_generate_profile] Error 2
make[1]: Leaving directory `/home/halfcoder/.local/src/Python-3.6.0-optmiz'
make: *** [profile-opt] Error 2