2014-10-02 43 views
0

我有一个Python脚本,它将通过一些包含压缩(QFS压缩,因此没有内置函数来处理它)十六进制数据的DAT文件来打开和解析。我在Github的C语言中找到了一个既能解压缩又能压缩的脚本,并且打算从Python中调用。为Python模块编译C代码的问题

Canopy的Python 2.7与我的Win8.1 64位机器编译C代码没有很大的关系。它确实编译时有错误,但是在调用时它会崩溃内核,然后我必须重新编译或者我得到一个dll错误(它找到了dll但不能使用它)。

所以我通过Anaconda安装了Python 3.4。它与我的机器很好,但在编译期间出现错误。

这是最后看来相关的C代码。缺少的代码是使用的实际算法。 代码本身可以在 https://github.com/wouanagaine/SC4Mapper-2013/blob/master/Modules/qfs.c 看到,如果你需要更多。如果你想看到它,setup.py代码在同一个“Modules”文件夹中。

static PyObject *QFSEncode(PyObject *self, PyObject *args) 
{ 
    char* buffer; 
    unsigned char* bufferIn; 
    int len; 
    unsigned char* out; 
    PyObject *pRet ; 
    if (!PyArg_ParseTuple(args, "s#", &buffer, &len)) 
     return NULL; 
    out = (unsigned char*)malloc(len*2); 
    bufferIn = (unsigned char*)malloc(len + 2000); 
    memcpy(bufferIn, buffer, len); 
    compress_data((unsigned char*)bufferIn, &len, out); 
    pRet = Py_BuildValue("s#", out, len); 
    free(out); 
    free(bufferIn); 
    return pRet; 
} 

static PyObject *QFSDecode(PyObject *self, PyObject *args) 
{ 
    char* buffer; 
    int len; 
    unsigned char* out; 
    PyObject *pRet ; 
    if (!PyArg_ParseTuple(args, "s#", &buffer, &len)) 
     return NULL; 
    out = uncompress_data((unsigned char*)buffer, &len); 
    pRet = Py_BuildValue("s#", out, len); 
    free(out); 
    return pRet; 
} 


static PyMethodDef QFSMethods[] = 
{ 
    {"decode", QFSDecode, METH_VARARGS, "decode a buffer" }, 
    {"encode", QFSEncode, METH_VARARGS, "encode a buffer" }, 
    {NULL, NULL, 0, NULL}  /* Sentinel */ 
}; 


PyMODINIT_FUNC initQFS(void) 
{ 
    (void) Py_InitModule("QFS", QFSMethods); 
} 

我得到这个回报,当我尝试编译

C:\Users\John\Desktop\DAT>python setup.py install 
running install 
running build 
running build_ext 
building 'QFS' extension 
C:\strawberry\c\bin\gcc.exe -mdll -O -Wall -IC:\Anaconda3\include -IC:\Ana 
\include -c qfs.c -o build\temp.win-amd64-3.4\Release\qfs.o 
qfs.c: In function 'QFSEncode': 
qfs.c:259:11: warning: pointer targets in assignment differ in signedness 
nter-sign] 
qfs.c: In function 'initQFS': 
qfs.c:293:5: warning: implicit declaration of function 'Py_InitModule' [-W 
it-function-declaration] 
qfs.c:294:1: warning: control reaches end of non-void function [-Wreturn-t 
qfs.c: In function 'compress_data': 
qfs.c:184:32: warning: 'bestoffs' may be used uninitialized in this functi 
maybe-uninitialized] 
writing build\temp.win-amd64-3.4\Release\QFS.def 
C:\strawberry\c\bin\gcc.exe -shared -s build\temp.win-amd64-3.4\Release\qf 
ild\temp.win-amd64-3.4\Release\QFS.def -LC:\Anaconda3\libs -LC:\Anaconda3\ 
d\amd64 -lpython34 -lmsvcr100 -o build\lib.win-amd64-3.4\QFS.pyd 
Cannot export PyInit_QFS: symbol not defined 
build\temp.win-amd64-3.4\Release\qfs.o:qfs.c:(.text+0x98b): undefined refe 
to `Py_InitModule' 
collect2.exe: error: ld returned 1 exit status 
error: command 'C:\\strawberry\\c\\bin\\gcc.exe' failed with exit status 1 

这是行259

bufferIn = (unsigned char*)malloc(len + 2000); 

我试着用指针瞎搞,但我不相信这样做这个。我确实设法让第一个错误消失,但我很确定我打破了代码功能。 虽然我仍然得到第二个错误。

我对Python并不陌生,但这是我第一次进入更高级别的Python编程。 我不知道任何C,如果它不太复杂,我可以勉强遵循一些代码。

+1

它看起来像'Py_InitModule()'是未定义的,在这种情况下,您可能忘记将#include '放在C文件的顶部。这可能会解决一些其他问题。 – 2014-10-02 20:21:28

+0

感谢您的快速回复。该位确实位于顶端#include #include #include joshua43214 2014-10-02 20:23:35

+0

然后,对您的系统有点麻烦。 (您确定*您已经正确安装了Python SDK吗?)检查Python.h文件是否真的存在于编译器的#include搜索路径的某处。 – 2014-10-02 20:38:48

回答