亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

C語(yǔ)言編寫(xiě)的Python模組載入時(shí)提示.so中的函數(shù)找不到?
怪我咯
怪我咯 2017-05-18 11:00:29
0
1
780

我嘗試透過(guò)C語(yǔ)言寫(xiě)一個(gè)Python的模組,但是我的C程式本身又依賴(lài)一個(gè)第三方的函式庫(kù)(libwiringPi.so),當(dāng)我在Python原始程式中import我產(chǎn)生的函式庫(kù)時(shí),會(huì)提示函數(shù)未定義,這些函數(shù)都是那個(gè)第三方函式庫(kù)裡的,我應(yīng)該怎麼編譯才能讓我編譯出的模組可以動(dòng)態(tài)連結(jié)那個(gè)函式庫(kù)?

我也嘗試過(guò)使用gcc手動(dòng)編譯動(dòng)態(tài)連結(jié)函式庫(kù),然後用ctyes,但是報(bào)一樣的錯(cuò)誤;產(chǎn)生模組的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 */
};

void
initdht11(void)
{
    PyImport_AddModule("dht11");
    Py_InitModule("dht11", dht11_methods);
}

setup.py

#
from distutils.core import setup, Extension

dht11module = 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 dht11
ImportError: /usr/local/lib/python2.7/dist-packages/dht11.so: undefined symbol: wiringPiSetup
怪我咯
怪我咯

走同樣的路,發(fā)現(xiàn)不同的人生

全部回覆(1)
左手右手慢動(dòng)作

哎,早上醒來(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, Extension

dht11module = Extension('dht11',
                    library_dirs = ['/usr/lib'], #指定庫(kù)的目錄
                    include_dirs = ['/usr/include'], #制定頭文件的目錄
                    libraries = ['wiringPi'], #指定庫(kù)的名稱(chēng)
                    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])
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板