C語(yǔ)言編寫(xiě)的Python模塊加載時(shí)提示.so中的函數(shù)未找到?
問(wèn)題描述
我嘗試通過(guò)C語(yǔ)言編寫(xiě)一個(gè)Python的模塊,但是我的C程序本身又依賴于一個(gè)第三方的庫(kù)(libwiringPi.so),當(dāng)我在Python源程序中import我生成的庫(kù)時(shí),會(huì)提示函數(shù)未定義,這些函數(shù)都是那個(gè)第三方庫(kù)里的,我應(yīng)該怎樣編譯才能讓我編譯出的模塊可以動(dòng)態(tài)鏈接那個(gè)庫(kù)?
我也嘗試過(guò)使用gcc手動(dòng)編譯動(dòng)態(tài)鏈接庫(kù),然后用ctyes,但是報(bào)一樣的錯(cuò)誤;生成模塊的C代碼和setup.py代碼都是基于Python源碼包中的demo程序。
我的C程序代碼
/* Example of embedding Python in another program */#include 'python2.7/Python.h'#include <wiringPi.h>void initdht11(void); /* Forward */int main(int argc, char **argv){ /* Initialize the Python interpreter. Required. */ Py_Initialize(); /* Add a static module */ initdht11(); /* Exit, cleaning up the interpreter */ Py_Exit(0); return 0;}/* A static module *//* ’self’ is not used */static PyObject *dht11_foo(PyObject *self, PyObject* args){ wiringPiSetup(); return PyInt_FromLong(42L);}static PyMethodDef dht11_methods[] = { {'foo', dht11_foo, METH_NOARGS, 'Return the meaning of everything.'}, {NULL, NULL} /* sentinel */};voidinitdht11(void){ PyImport_AddModule('dht11'); Py_InitModule('dht11', dht11_methods);}
setup.py
from distutils.core import setup, Extensiondht11module = Extension(’dht11’, library_dirs = [’/usr/lib’], include_dirs = [’/usr/include’], sources = [’math.c’])setup (name = ’dht11’, version = ’1.0’, description = ’This is a demo package’, author = ’Martin v. Loewis’, author_email = ’martin@v.loewis.de’, url = ’https://docs.python.org/extending/building’, long_description = ’’’This is really just a demo package.’’’, ext_modules = [dht11module])
錯(cuò)誤信息
Traceback (most recent call last): File 'test.py', line 1, in <module> import dht11ImportError: /usr/local/lib/python2.7/dist-packages/dht11.so: undefined symbol: wiringPiSetup
問(wèn)題解答
回答1:哎,早上醒來(lái)突然想到,趕緊試了一下。
出現(xiàn)這個(gè)問(wèn)題是因?yàn)樵诰幾g的時(shí)候需要加 -lwiringPi 選項(xiàng)來(lái)引用這個(gè)庫(kù),但是我仔細(xì)看了以下執(zhí)行 python setup.py build 之后執(zhí)行的編譯命令,根本就沒(méi)有加這個(gè)選項(xiàng),解決方式很簡(jiǎn)單,只需要修改一下setup.py,在Extension里面加上 libraries = [’wiringPi’] 這個(gè)參數(shù)就行了,修改后的setup.py變成如下樣子
from distutils.core import setup, Extensiondht11module = Extension(’dht11’, library_dirs = [’/usr/lib’], #指定庫(kù)的目錄 include_dirs = [’/usr/include’], #制定頭文件的目錄 libraries = [’wiringPi’], #指定庫(kù)的名稱 sources = [’math.c’])setup (name = ’dht11’, version = ’1.0’, description = ’This is a demo package’, author = ’Martin v. Loewis’, author_email = ’martin@v.loewis.de’, url = ’https://docs.python.org/extending/building’, long_description = ’’’This is really just a demo package.’’’, ext_modules = [dht11module])
相關(guān)文章:
1. docker內(nèi)創(chuàng)建jenkins訪問(wèn)另一個(gè)容器下的服務(wù)器問(wèn)題2. 如何解決Centos下Docker服務(wù)啟動(dòng)無(wú)響應(yīng),且輸入docker命令無(wú)響應(yīng)?3. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?4. html - css 使用字體的時(shí)候,格式有什么特殊要求嗎?5. html5 - 百度echart官網(wǎng)下載的地圖json數(shù)據(jù)亂碼6. css3 - 學(xué)習(xí)css構(gòu)建圖形時(shí),遇到一個(gè)很有意思的現(xiàn)象,具體代碼如下7. javascript - js閉包作用域8. javascript - echart+百度地圖9. 極光推送 - Android app消息推送 百度 極光 個(gè)推 信鴿哪個(gè)好一些?10. 微信開(kāi)放平臺(tái) - android 微信支付后點(diǎn)完成按鈕,后回調(diào)打開(kāi)第三方頁(yè)面,屏幕閃動(dòng),求解決方法
