2017-10-17 1 views
1

Ich versuche, ein Python-Modul mit C und zu einem Thema stecken zu bauen:Benutzerdefinierte C Modul enthält

Wenn ich zusätzliche Header-Datei (test.h), Modul ohne Warnungen kompiliert, aber später import dieses Modul , Beschwert sich Python über das undefinierte Symbol maketest.

Meine Modulstruktur sieht wie folgt aus (das Modul spam genannt wird):

spam\ 
    ├spammodule.c 
    |setup.py 
    ├─src\ 
     |── test.c 
     |── test.h 

Inhalt des spammodule.c:

#include <Python.h> 
#include "test.h" 

static PyObject * SpamError; 

static PyObject * 
spam_system(PyObject *self, PyObject *args) 
{ 
    const char *command; 
    int sts; 

    maketest(); // <---- calling function from additional file 

    if(!PyArg_ParseTuple(args, "s", &command)) 
     return NULL; 

    sts = system(command); 
    if (sts < 0) { 
     PyErr_SetString(SpamError, "System command failed"); 
     return NULL; 
    } 
    return PyLong_FromLong(sts); 
} 

PyMODINIT_FUNC 
initspam(void) 
{ 
    PyObject *m; 
    static PyMethodDef SpamMethods[] = { 
     {"system", spam_system, METH_VARARGS, "Execute a shell command."}, 
     {NULL, NULL, 0, NULL} 
    }; 
    m = Py_InitModule("spam", SpamMethods); 
    if (m == NULL) 
     return; 

    SpamError = PyErr_NewException("spam.error", NULL, NULL); 
    Py_INCREF(SpamError); 
    PyModule_AddObject(m, "error", SpamError); 
} 

int 
main(int argc, char *argv[]) 
{ 
    Py_SetProgramName(argv[0]); 
    Py_Initialize(); 
    initspam(); 
} 

Inhalt von setupy.py:

from setuptools import setup,Extension 

spam_module = Extension('spam', 
     sources = ['spammodule.c'], 
     include_dirs=['src/'],) 

setup (name = 'Spam', 
    version = '1.0', 
    description = 'Sample module.', 
    ext_modules = [ spam_module ]) 

Inhalt von src/test.h :

void maketest(void); 

Inhalt von src/test.c:

#include "test.h" 

void maketest() { 
    printf("test passed"); 
} 

ich kompilieren alles mit python setup.py build, nach dem Ausführen von Python-Prompt ich versuche, mein Modul zu importieren und den Fehler:

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import spam 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: ./spam.so: undefined symbol: maketest 

Hat ein jemand Ahnung, was ist los?

+1

Sie sind absolut nirgendwo * in der Objektdatei von 'src/test.c' in die' .so' verlinken. Fügen Sie der Quellenliste 'src/test.c' hinzu. –

+0

Sie haben Recht, ich dachte, dass einschließlich 'src/*' Dateien ist genug, aber nein, wie Sie sagten, zusätzliche Dateien sollten in Quellen Tupel aufgeführt werden, danke. Ich habe eine Antwort auf meinen Beitrag hinzugefügt – user3459276

+0

Nein, Sie sollten * Ihre Antwort * als * Antwort * post *. Dann * akzeptiere es *! –

Antwort

0

ok Jungs dank Antii für Anhaltspunkt, wie Sie sagten Problem war, dass test.c in Quellen aufgenommen werden sollten TUPLE so modifiziert i setup.py und alles funktioniert jetzt es so look`s:

from setuptools import setup,Extension 

spam_module = Extension('spam', 
     sources = ['spammodule.c','src/test.c'], 
     include_dirs=['src/']) 

setup (name = 'Spam', 
    version = '1.0', 
    description = 'Sample module.', 
    ext_modules = [ spam_module ])