av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

瀏覽:130日期: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 = ’martin@v.loewis.de’, 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 = ’martin@v.loewis.de’, url = ’https://docs.python.org/extending/building’, long_description = ’’’This is really just a demo package.’’’, ext_modules = [dht11module])

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩精品一区二区三区在线观看 | 国产精品国产三级国产aⅴ中文 | 欧美精品在线观看 | 一级毛片视频在线观看 | 美女一级毛片 | 日韩欧美国产精品一区 | 日韩有码一区 | 91精品国产91久久久久游泳池 | 中文字幕在线播放第一页 | 婷婷色在线 | 最新日韩在线 | 久久久精品久 | 欧美一级电影免费 | 羞羞视频免费观 | 久久久www成人免费精品张筱雨 | 2020亚洲天堂 | a在线观看免费 | 精品一区二区观看 | 一区二区精品 | 中文字幕一区二区三区精彩视频 | 免费污视频 | 久久久久久国产精品免费 | 伊人超碰在线 | 亚洲福利 | 久久精品小短片 | 亚洲啪啪 | 亚洲人成人一区二区在线观看 | 91亚洲一区 | 国产成人精品a视频 | 久久亚洲一区二区 | 日韩欧美国产一区二区三区 | 开操网 | 久久久蜜桃 | 91精品国产日韩91久久久久久 | 亚洲午夜久久久 | 一色桃子av一区二区 | 日韩国产一区二区三区 | 亚洲视频在线免费观看 | 欲色av| 成人在线一区二区三区 | 青青草av |