色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

C語言編寫的Python模塊加載時提示.so中的函數未找到?

瀏覽:118日期:2022-07-02 08:45:16

問題描述

我嘗試通過C語言編寫一個Python的模塊,但是我的C程序本身又依賴于一個第三方的庫(libwiringPi.so),當我在Python源程序中import我生成的庫時,會提示函數未定義,這些函數都是那個第三方庫里的,我應該怎樣編譯才能讓我編譯出的模塊可以動態鏈接那個庫?

我也嘗試過使用gcc手動編譯動態鏈接庫,然后用ctyes,但是報一樣的錯誤;生成模塊的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 = ’[email protected]’, url = ’https://docs.python.org/extending/building’, long_description = ’’’This is really just a demo package.’’’, ext_modules = [dht11module])

錯誤信息

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

問題解答

回答1:

哎,早上醒來突然想到,趕緊試了一下。

出現這個問題是因為在編譯的時候需要加 -lwiringPi 選項來引用這個庫,但是我仔細看了以下執行 python setup.py build 之后執行的編譯命令,根本就沒有加這個選項,解決方式很簡單,只需要修改一下setup.py,在Extension里面加上 libraries = [’wiringPi’] 這個參數就行了,修改后的setup.py變成如下樣子

from distutils.core import setup, Extensiondht11module = Extension(’dht11’, library_dirs = [’/usr/lib’], #指定庫的目錄 include_dirs = [’/usr/include’], #制定頭文件的目錄 libraries = [’wiringPi’], #指定庫的名稱 sources = [’math.c’])setup (name = ’dht11’, version = ’1.0’, description = ’This is a demo package’, author = ’Martin v. Loewis’, author_email = ’[email protected]’, url = ’https://docs.python.org/extending/building’, long_description = ’’’This is really just a demo package.’’’, ext_modules = [dht11module])

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产资源免费 | 草草久久97超级碰碰碰免费 | 免费人成观看在线网 | 亚洲成人91 | 国产成人综合精品一区 | 欧美一级艳片视频免费观看 | 欧美另类高清xxxxx | 亚洲一在线 | 日韩中文在线 | 特黄特黄aaaa级毛片免费看 | 国产午夜一级淫片 | 成年日韩片av在线网站 | 国产伦精品一区二区三区四区 | 日日摸人人看97人人澡 | 久久久久久久久免费影院 | 成人涩涩屋福利视频 | 免费观看呢日本天堂视频 | a级毛片免费在线观看 | 在线观看中文字幕一区 | 国产偷国产偷亚洲高清午夜 | 欧美精品日日鲁夜夜添 | 一区二区在线看 | 欧美成人免费sss | 中国国产成人精品久久 | 日本乱人伦片中文字幕三区 | 亚洲综合色自拍一区 | 日产国产精品亚洲系列 | 日本一区二区高清免费不卡 | 怡红院视频在线 | 思思99re| 久久精品国内偷自一区 | 亚洲精品大片 | 一级黄色香蕉视频 | 日本韩国一区 | 69福利网 | 90岁老太婆一级毛片 | 国产一区二区久久 | 亚洲成人一级 | 久久久9视频在线观看 | 亚洲欧美网 | 91精品最新国内在线播放 |