2014-10-02 41 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

回答