首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译Python模块的C代码时出现问题

编译Python模块的C代码时出现问题
EN

Stack Overflow用户
提问于 2014-10-03 04:10:47
回答 1查看 301关注 0票数 0

我有一个Python脚本,它将打开和解析一些DAT文件,其中包含压缩的(QFS压缩,所以没有内置的函数来处理)十六进制数据。我在Github的C语言中找到了一个脚本,它既可以解压缩,也可以重新压缩,并且打算从Python中调用。

Canopy的Python2.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代码,则它位于相同的“模块”文件夹中。

代码语言:javascript
复制
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);
}

当我试图编译它时,我得到了这个返回

代码语言:javascript
复制
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行

代码语言:javascript
复制
bufferIn = (unsigned char*)malloc( len + 2000 );

我试着摆弄指针,但我对此没有信心。我确实设法让第一个错误消失了,但我很确定我这样做破坏了代码的功能。不过,我还是得到了第二个错误。

我对Python并不陌生,但这是我第一次尝试更高级的Python编程。我不懂任何C语言,如果不是太复杂的话,我只能勉强跟上一些代码。

EN

回答 1

Stack Overflow用户

发布于 2015-05-29 17:24:16

Python3中不存在Py_InitModule(),它或多或少被PyModule_Create()所取代。参见Porting Extension Modules to Python 3

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26168842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档