c++作為模塊擴(kuò)展python

具體參考這里:基于 Boost 庫(kù)實(shí)現(xiàn)的 Python 調(diào)用 C++ 接口

錯(cuò)誤1:undefined symbol: PyUnicodeUCS2_AsUTF8String

原因:
c++編譯采用的是ucs2,系統(tǒng)python用的是ucs4(可以通過(guò)sys.maxunicode查看是否大于65525)
參考:http://effbot.org/pyfaq/when-importing-module-x-why-do-i-get-undefined-symbol-pyunicodeucs2.htm

辦法:
重新編譯一個(gè)python或者重新編譯你的庫(kù)(csdn有個(gè)人提到編譯庫(kù)的方法,但是沒(méi)說(shuō)具體怎么編譯,不知道怎樣加入usc2,沒(méi)找到方法)

cd Python-2.7.3- 
./configure --prefix=/home/xxx/opt/python2.7 --enable-unicode=ucs2 && make
sudo make install

這樣會(huì)在/home/xxx/opt/python2.7下生成ucs2的python版本,重新指定Makefile的python路徑,make,

錯(cuò)誤2: 提示:

g++ -std=c++11 -I/opt/include/python2.7 -I/home/zishuai/boost/include -fPIC -o foo.o -c foo.cc
g++ -std=c++11 -I/opt/include/python2.7 -I/home/zishuai/boost/include -fPIC -o pymodule.o -c pymodule.cc
g++ -std=c++11 -shared -Wl,--export-dynamic foo.o pymodule.o -L/home/zishuai/boost/lib -lboost_python -L/opt/lib/python2.7/config -lpython2.7 -o foo.so
/usr/bin/ld: /opt/lib/python2.7/config/libpython2.7.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
/opt/lib/python2.7/config/libpython2.7.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [foo.so] Error 1

意思是:編譯python時(shí)要指定 -fPIC,怎么指定呢?看python文件夾下的./configure 有個(gè)--enable-shared,就是它:

./configure --prefix=/opt --enable-unicode=ucs2 --enable-shared && make
然后sudo make install
終于成功了?。。?/p>

warning: 再次make c++工程:提示:

g++ -std=c++11 -I/opt/include/python2.7 -I/home/zishuai/boost/include -fPIC -o foo.o -c foo.cc
g++ -std=c++11 -I/opt/include/python2.7 -I/home/zishuai/boost/include -fPIC -o pymodule.o -c pymodule.cc
g++ -std=c++11 -shared -Wl,--export-dynamic foo.o pymodule.o -L/home/zishuai/boost/lib -lboost_python -L/opt/lib/python2.7/config -lpython2.7 -o foo.so
/opt/lib/python2.7/config/libpython2.7.a(posixmodule.o): In function `posix_tmpnam':
/home/zishuai/Downloads/Python-2.7.13/./Modules/posixmodule.c:7614: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
/opt/lib/python2.7/config/libpython2.7.a(posixmodule.o): In function `posix_tempnam':
/home/zishuai/Downloads/Python-2.7.13/./Modules/posixmodule.c:7561: warning: the use of `tempnam' is dangerous, better use `mkstemp'

python3.x 把tmpnam_r移除了(http://bugs.python.org/issue17880),不用管這個(gè)警告,沒(méi)事的,或者選用3.x版本。

錯(cuò)誤3:error while loading shared libraries: ./libc.so.6: ELF file OS ABI invalid

原因:1. 我把虛擬機(jī)編譯好的.so庫(kù)copy到服務(wù)器上了,服務(wù)器和虛擬機(jī)版本不一致,各種庫(kù)的不一致,導(dǎo)致這個(gè)問(wèn)題。

  1. 在服務(wù)器編譯.so后,運(yùn)行如果提示這個(gè)錯(cuò)誤的話(huà),那就是服務(wù)器python版本的編譯版本太老了,運(yùn)行 python:
Python 2.7.3 (default, Dec 23 2016, 16:48:53) 
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

是gcc 3.4.5編譯的,太老了,重新編譯個(gè)python
版本,方法見(jiàn)上。

錯(cuò)誤4: ImportError: /lib64/tls/libc.so.6: versionGLIBC_2.14' not found (required by /opt/compiler/gcc-4.8.2/lib/libstdc++.so.6)`

原因不明,可能是因?yàn)閜ython版本問(wèn)題,解決中。。。。


果然還是python編譯版本問(wèn)題。

錯(cuò)誤5: ImportError: libboost_python.so.1.62.0: cannot open shared object file: No such file or directory

.bashrc中配置:

LD_LIBRARY_PATH=/xxx/boost/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

然后在當(dāng)前運(yùn)行python腳本的終端 source下.bashrc。

出了這么多問(wèn)題~~~~

圖片來(lái)自網(wǎng)絡(luò)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容