简体   繁体   English

使用swig将c ++编译为python

[英]Compile c++ to python using swig

I'm trying to compile very simple file to python on windows and I'm having a bad time. 我正在尝试在Windows上编译非常简单的文件到python,我的时间很糟糕。

The .i file is testfile.i: .i文件是testfile.i:

%module testfile

%include "stl.i"

%{  
    int get_num() {
        return 3;
    }
%}  

int get_num() {
        return 3;
}

The swig function: {swig path}\\swig.exe -c++ -python testfile.i swig函数:{swig path} \\ swig.exe -c ++ -python testfile.i

This works perfectly, I now got the testfile.py file and testfile_wrap.cxx file. 这很好用,我现在得到了testfile.py文件和testfile_wrap.cxx文件。 Now I understood that I need to compile this to library (.pyd on windows). 现在我明白我需要将它编译到库(Windows上的.pyd)。 I tried: 我试过了:

{gcc path}\\gcc.exe -fPIC -shared testfile_wrap.cxx -o testfile_wrap.pyd -L. {gcc path} \\ gcc.exe -fPIC -shared testfile_wrap.cxx -o testfile_wrap.pyd -L。 -LC:\\Python27\\libs\\ -lpython27 -IC:\\python27\\include. -LC:\\ Python27 \\ libs \\ -lpython27 -IC:\\ python27 \\ include。

Here is the problem, I get alot of errors like this ones: 这是问题,我得到了很多像这样的错误:

C:\Users\itay\AppData\Local\Temp\ccANsNeU.o:testfile_wrap.cxx:(.text+0xc00): undefined reference to `__imp_PyExc_MemoryError'
C:\Users\itay\AppData\Local\Temp\ccANsNeU.o:testfile_wrap.cxx:(.text+0xc13): undefined reference to `__imp_PyExc_IOError'
C:\Users\itay\AppData\Local\Temp\ccANsNeU.o:testfile_wrap.cxx:(.text+0xc26): undefined reference to `__imp_PyExc_RuntimeError'
C:\Users\itay\AppData\Local\Temp\ccANsNeU.o:testfile_wrap.cxx:(.text+0xc39): undefined reference to `__imp_PyExc_IndexError'

And it continues on and on. 它一直在继续。

What am I doing wrong? 我究竟做错了什么?

Thank you for your help 谢谢您的帮助

Update: I managed to call swig and compile/link using visual studio 2013 but I get the same error. 更新:我设法使用visual studio 2013调用swig和编译/链接但我得到了同样的错误。 I followed tutorials and it still does not work. 我按照教程,它仍然无法正常工作。

The solution: my python was 64 bit and it didn't work. 解决方案:我的python是64位,它不起作用。 Changed to python 32 bit and now it is working (python 2.7.10) 改为python 32位,现在它正在工作(python 2.7.10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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