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

correctly making setup.py for c wrapper

我正在尝试设置一个 setup.py 文件设置一个 python 围绕一些简单的 C 代码的包装器。目前这就是我的 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])

这是我的 C 代码:

#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

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

头文件不是库。

如果您从 setup.py 中完全删除 include_dirslibraries 设置,您的扩展应该可以正确编译。如果仍然无法正常工作,请 post 您收到的一组新错误消息。