cx_Oracle是python连Oracle常用的lib,今天介绍如何使用静态链接的方式build cx_Oracle,避免对oracle共享库的依赖。
首先需要生成oracle客户端的静态lib:
cd $ORACLE_HOME/bin
./genclntst
ar: creating /u01/app/oracle/product/11.2.0/db_1/lib/libclntst11.a
Created /u01/app/oracle/product/11.2.0/db_1/lib/libclntst11.a
再build cx_Oracle
python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/u01/app/oracle/product/11.2.0/db_1/rdbms/demo -I/u01/app/oracle/product/11.2.0/db_1/rdbms/public -I/usr/include/python2.6 -c cx_Oracle.c -o build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.2.1
creating build/lib.linux-x86_64-2.6-11g
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -L/u01/app/oracle/product/11.2.0/db_1/lib -L/usr/lib64 -lclntsh -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
最后一行命令,使用了-lclntsh来链接Oracle的客户端动态库,我们需要把这个改成静态lib就可以了
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o /u01/app/oracle/product/11.2.0/db_1/lib/libclntst11.a -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
再运行
python setup.py install
running install
running bdist_egg
...
creating 'dist/cx_Oracle-5.2.1-py2.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
在dist目录下生成了cx_Oracle-5.2.1-py2.6-linux-x86_64.egg,这个就是可以直接使用的cx_Oracle,对oracle动态库没有依赖。