亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

問題描述

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

我也嘗試過使用gcc手動編譯動態(tài)鏈接庫,然后用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:

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

出現(xiàn)這個問題是因?yàn)樵诰幾g的時候需要加 -lwiringPi 選項來引用這個庫,但是我仔細(xì)看了以下執(zhí)行 python setup.py build 之后執(zhí)行的編譯命令,根本就沒有加這個選項,解決方式很簡單,只需要修改一下setup.py,在Extension里面加上 libraries = [’wiringPi’] 這個參數(shù)就行了,修改后的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])

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美一级毛片在线看视频 | 亚洲欧美日韩中文高清一 | 亚洲欧美精品一区二区 | 91蝌蚪在线播放 | 中国特级黄一级真人毛片 | 欧美在线黄色片 | 日本理论在线观看被窝网 | 国产精品久久久久免费a∨ 国产精品久久久久这里只有精品 | 国产成人综合洲欧美在线 | 国产一 | 在线成人免费视频 | 视频精品一区二区三区 | 午夜男人视频 | 国产亚洲视频网站 | 国产精品亚洲专区在线观看 | 久久人体视频 | 成年人黄色毛片 | 国产精品亚洲综合天堂夜夜 | 国产综合色在线视频区色吧图片 | 一区二区三区在线 | 日本 | 国产久草视频在线 | 国产线路一 | 青青青久在线视频免费观看 | 可以看黄色的网址 | 狠狠色欧美亚洲狠狠色五 | 亚洲 欧美 国产 日韩 制服 bt | 欧美一级视频免费看 | 免费一级视频在线播放 | 国产精品久久久久久一区二区三区 | 国产三级在线观看免费 | 免费视频一级片 | 欧美综合亚洲 | 在线成h人视频网站免费观看 | 91久久香蕉青青草原娱乐 | 香港aa三级久久三级不卡 | 国产精品区一区二区免费 | 亚洲麻豆精品果冻传媒 | 一区免费视频 | 久草视频在线首页 | 国产一区二区在线看 | 在线视频一二三区 |