python - 使用 swig 将 c++ 编译为 python

标签 python c++ gcc

我正在尝试在 Windows 上将非常简单的文件编译为 python,但我遇到了麻烦。

.i 文件是 testfile.i:

%module testfile

%include "stl.i"

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

int get_num() {
        return 3;
}

swig函数: {swig 路径}\swig.exe -c++ -python testfile.i

这非常有效,我现在得到了 testfile.py 文件和 testfile_wrap.cxx 文件。 现在我明白我需要将其编译为库(Windows 上的 .pyd)。我试过:

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

这是问题所在,我遇到了很多这样的错误:

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'

它一直在继续。

我做错了什么?

谢谢你的帮助

更新: 我设法使用 visual studio 2013 调用 swig 和编译/链接,但我得到了同样的错误。我按照教程进行操作,但仍然无法正常工作。

最佳答案

解决方案:我的 python 是 64 位的,它不工作。更改为 python 32 位,现在它正在工作(python 2.7.10)

关于python - 使用 swig 将 c++ 编译为 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524867/

相关文章:

c - 解决 "variable ' xxx 的最佳方法已声明但从未被引用”

python - 同步访问时,在共享内存中使用 numpy 数组会很慢

c++ - 使用<filesystem>获取文件的基本名称

c - GCC 作为 m68k 交叉编译器

c++ - 我可以使用哪些设计模式?

c++ - 如果某些类继承了抽象基类,则确定何时在某些类上使用函数

c++ - GCC 4.7 编译的库是否与 GCC 4.6.3 程序兼容?

python - 使用 Python 2.7 使用十六进制符号对字符串中的特定字符进行编码/解码

python - React - 具有动态 'output' 的 Django webpack 配置

python - 图中 3 点之间的平滑曲线