Beim Laden eines in der Sprache C geschriebenen Python-Moduls wird angezeigt, dass die Funktion in .so nicht gefunden wird?
怪我咯
怪我咯 2017-05-18 11:00:29
0
1
518

Ich habe versucht, ein Python-Modul in der Sprache C zu schreiben, aber mein C-Programm selbst hängt von einer Bibliothek eines Drittanbieters ab (libwiringPi.so). Wenn ich die Bibliothek importiere, die ich im Python-Quellprogramm generiert habe, werde ich aufgefordert, die Funktion auszuführen wurde nicht definiert, diese Funktionen befinden sich alle in dieser Bibliothek eines Drittanbieters. Wie soll ich sie kompilieren, damit das von mir kompilierte Modul dynamisch mit dieser Bibliothek verknüpft werden kann?

Ich habe auch versucht, die dynamische Linkbibliothek mit gcc manuell zu kompilieren, und habe dann cyes verwendet, aber der C-Code und der setup.py-Code des generierten Moduls basieren beide auf dem Demoprogramm in der Python-Quelle Codepaket.

Mein C-Programmcode

/* 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])

Fehlermeldung

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
怪我咯
怪我咯

走同样的路,发现不同的人生

Antworte allen(1)
左手右手慢动作

哎,早上醒来突然想到,赶紧试了一下。

出现这个问题是因为在编译的时候需要加 -lwiringPi 选项来引用这个库,但是我仔细看了以下执行 python setup.py build 之后执行的编译命令,根本就没有加这个选项,解决方式很简单,只需要修改一下setup.py,在Extension里面加上 libraries = ['wiringPi'] 这个参数就行了,修改后的setup.py变成如下样子

from distutils.core import setup, Extension

dht11module = 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])
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!