系统自带的python有点老(2.6)于是下载python2.7.8的源码手动编译安装(编译参数./configure --enable-shared --disable-ipv6 --prefix=/usr/local/python27 --enable-unicode=ucs4)

安装后将系统的python替换(ln -svf /usr/local/python27/bin/* /usr/bin/), 安装easy_install和pip(easy_install pip), 之后开始安装pyopenssl, 但是在安装依赖cffi的时候报错**/usr/bin/ld: cannot find -lpython2.7**.

首先需要说明的是出现类似**/usr/bin/ld: cannot find -lxx**的原因主要是gcc没有找到libxx.so的文件

[root@ubox ~]# echo '/usr/local/python27/lib' > /etc/ld.so.conf.d/python-2.7.8.conf
[root@ubox ~]# ldconfig -v

ldconfig命令为重建动态链接库的缓存.然后重新执行pip install cffi, 但是还是报错, 使用gcc -lpython2.7 --verbose查看gcc查找库的路径

[root@ubox lib]# gcc -lpython2.7 --verbose
Using built-in specs.
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-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/:/usr/libexec/gcc/x86_64-redhat-linux/4.4.7/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mtune=generic'
 /usr/libexec/gcc/x86_64-redhat-linux/4.4.7/collect2 --eh-frame-hdr --build-id -m elf_x86_64 --hash-style=gnu -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../.. -lpython2.7 -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.4.7/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/crtn.o
/usr/bin/ld: cannot find -lpython2.7
collect2: ld returned 1 exit status

可以看到**/usr/local/python27/lib**并不在上面的路径, google一下找到了这个答案http://stackoverflow.com/questions/8400272/usr-bin-ld-cannot-find-lpython2-7, 然后

[root@micaopan-dev lib]# locate libpython2.7.a
/usr/local/python27/lib/python2.7/config/libpython2.7.a
/usr/local/src/Python-2.7.8/libpython2.7.a
[root@micaopan-dev lib]# ln -s /usr/local/python27/lib/python2.7/config/libpython2.7.a /usr/lib
[root@micaopan-dev lib]# gcc -lpython2.7 --verbose
...
(.text+0x20): undefined reference to 'main'
collect2: ld returned 1 exit status

这个gcc找到了动态链接库, 报错根据http://stackoverflow.com/questions/15905119/c-linking-error-undefined-reference-to-main应该并没有什么大碍

然后执行pip install cffi成功.

PS: 参考

  1. /usr/bin/ld: cannot find -l* 错误的解决方法……
  2. 关于usr/bin/ld: cannot find -lxxx问题总结
  3. /usr/bin/ld: cannot find -lpython2.7
  4. C Linking Error: undefined reference to 'main'