正确地为c包装器制作setup.py

时间:2018-03-07 15:13:47

标签: python c linux wrapper

我正在尝试设置一个setup.py文件,围绕一些简单的c代码设置一个python包装器。目前这是我的setup.py:

from distutils.core import setup, Extension

module1 = Extension('ledmod', include_dirs = ['/usr/include', '/usr/include/arm-linux-gnueabi'],
    libraries = ['fcntl.h','sys/mman.h','sys/stat.h','sys/types.h','unistd.h'],
    sources=['ledmodule.c'])

setup (name = 'ledmod',
    version = '1.0',
    description = "LED example for TS7400v2",
    author='author',
    url='site',
    ext_modules=[module1])

这是我的代码:

#include <Python.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
//Define a new exception object for our module
static PyObject *ledmodError;


static PyObject* ledmodset(PyObject* self, PyObject *args){
    short state;

    if(!PyArg_ParseTuple(args, "h", &state)){
        return NULL;
    }
    if(state > 1 || state < 0){
        PyErr_SetString(ledmodError, "state must be 0 or 1");
    }

    unsigned int *pinctl = NULL;
    unsigned int devmem = open("/dev/mem", O_RDWR|O_SYNC);
    pinctl = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, devmem, 0x80018000); //0x80010800 is beginning of PinCtrl see page 722 of man for memory map
    pinctl[(0x114)>>2] = (0x3 << (24));//24 Bank 0_PIN28 Check page 729 of man and 717 for enable (0x3)
    if(state == 1){
        pinctl[(0x708)>>2] = (0x1 << 28);//SET See page 838 of man
    }else{
        pinctl[(0x704)>>2] = (0x1 << 28);//SET See page 838 of man
    }

    pinctl[(0xb04)>>2] = (0x1 << 28); //Enable Register. See page 847

    return NULL;
}

static PyMethodDef ledmod_methods[] = {
    //"PythonName" C-funtion Name, argument presentaion, description
    {"led_set", ledmodset,  METH_VARARGS, "Sets the green led of the TS7400v2 based on state input"},
    {NULL,NULL,0,NULL}
};

PyMODINIT_FUNC initexmod(void){
    PyObject *m;
    m = Py_InitModule("ledmod", ledmod_methods);
    if(m == NULL) return;

    ledmodError = PyErr_NewException("ledmod.error", NULL, NULL);//"exmod.error" python error object
    Py_INCREF(ledmodError);
    PyModule_AddObject(m, "error",ledmodError);
}

然而,似乎我无法使用sudo python setup.py build进行构建,因为我收到以下错误:

running build
running build_ext
building 'ledmod' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include -I/usr/include/arm-linux-gnueabi -I/usr/include/python2.7 -c ledmodule.c -o build/temp.linux-armv5tejl-2.7/ledmodule.o
ledmodule.c: In function 'ledmodset':
ledmodule.c:25:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
warning: no library file corresponding to 'sys/mman.h' found (skipping)
warning: no library file corresponding to 'sys/stat.h' found (skipping)
warning: no library file corresponding to 'sys/types.h' found (skipping)
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv5tejl-2.7/ledmodule.o -lfcntl.h -lunistd.h -o build/lib.linux-armv5tejl-2.7/ledmod.so
/usr/bin/ld: cannot find -lfcntl.h
/usr/bin/ld: cannot find -lunistd.h
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

我错过了什么?我列出了库和include目录......但是从错误中找不到库。

1 个答案:

答案 0 :(得分:0)

头文件不是库。

如果您从setup.py中完全删除了include_dirslibraries设置,则您的扩展程序应该正确编译。如果它仍然无效,请发布您收到的新错误消息。

相关问题