简体   繁体   中英

Issues compiling C code for a Python module

I have a Python script that will open and parse through some DAT files that contain among other things compressed (QFS compression, so no built function to deal with it) hex data. I found a script in C at Github that does both decompression, and recompression, and is intended to be called from Python.

Canopy's Python 2.7 does no play nice with my Win8.1 64 bit machine for compiling C code. It does compile with errors, but it crash the kernel when called, and then I have to recompile or I get a dll error (it finds the dll but can't use it).

So I installed Python 3.4 via Anaconda. It plays nice with my machine but I get errors during compilation.

Here is what seems to be the relevant C code at the end. The missing code is the actual algorithm used. the code itself can be seen at https://github.com/wouanagaine/SC4Mapper-2013/blob/master/Modules/qfs.c if you need more of it. The setup.py code is in the same "Modules" folder if you wish to see it.

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);
}

I get this return when I try to compile it

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

this is line 259

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

I tried messing around with the pointer, but I am not confident about doing this. I did manage to get the first error to go away, but I am pretty sure I broke the codes functionality doing so. I still get the second error though.

I am not new to Python, but this is my first foray into higher level Python programing. I do not know any C, I can just barely follow along some code if it is not too complex.

Py_InitModule() doesn't exist in Python 3, it's more or less replaced by PyModule_Create() . See Porting Extension Modules to Python 3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM