问题:使用SWIG、mingw32和distutils时遇到的问题。

3
我一直在尝试在Windows 7下设置Python 2.7环境,以便我可以编译一个C++扩展用于Python。因为我是新手,所以我下载了一个简单的示例(此处),并使用了原始文件。我的路径中还有一个numpy.i文件。我已经安装了mingw(最新版本)和swig(v. 3.0.10),我的Python版本是2.7.9。我甚至使用这个环境编译了一个小的C++程序,没有问题。
但是,当尝试构建上述“简单”的Python扩展时,我总是得到以下输出,指示失败(我在Windows cmd.exe窗口中发出的命令作为下面第一行)。
python setup.py build -c=mingw32
running build
running build_ext
building '_simple' extension
swigging simple.i to simple_wrap.c
C:\swigwin\swigwin-3.0.10\swig.exe -python -o simple_wrap.c simple.i
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\site-packages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple.cc -o build\temp.win32-2.7\Release\simple.o
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\sitepackages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple_wrap.c -o build\temp.win32-2.7\Release\simple_wrap.o
writing build\temp.win32-2.7\Release\_simple.def
C:\MinGW\bin\g++.exe -shared -s build\temp.win32-2.7\Release\simple.o build\temp.win32-2.7\Release\simple_wrap.o build\temp.win32-2.7\Release\_simple.def -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lmsvcr90 -o build\lib.win32-2.7\_simple.pyd
build\temp.win32-2.7\Release\simple_wrap.o:simple_wrap.c:(.text+0xce5): undefined reference to `create_list'
build\temp.win32-2.7\Release\simple_wrap.o:simple_wrap.c:(.text+0x170d): undefined reference to `dot'
collect2.exe: error: ld returned 1 exit status
error: command 'C:\\MinGW\\bin\\g++.exe' failed with exit status 1

我有一种可怕的感觉,觉得我可能错过了非常简单的东西,但在另一个Cygwin环境中,我已经成功编译了这些相同的文件,并且没有任何问题(是的,我希望能够在一个非Cygwin的环境中执行此操作)。

我不想用太多代码来描述这个问题,但为了参考,这里是我正在使用的simple.isetup.py文件。

simple.i:
%module simple
%{
  #define SWIG_FILE_WITH_INIT
  #include "simple.h"
%}

%include "numpy.i"
%init %{
import_array();
%}

%apply (int DIM1, double* INPLACE_ARRAY1) {(int n0, double *a0)};
%apply (int DIM1, double* IN_ARRAY1) {(int n, double *a), (int m, double *b)};
%apply (int DIM1, double* ARGOUT_ARRAY1) {(int size, double *arr)};
%include "simple.h"

setup.py:
from distutils.core import setup, Extension
import numpy
import os

os.environ['CC'] = 'g++';
setup(name='matt_simple_test', version='1.0', ext_modules =[Extension('_simple',['simple.cc', 'simple.i'], include_dirs = [numpy.get_include(),'.'])])

如果需要其他代码,我很乐意发布它们,但是其他文件(simple.ccsimple.h)均从此处直接使用。所以问题是:有人能指导我如何纠正这个错误吗?

你确定MingW和Python都是32位的吗? - Jens Munk
我使用了标准的mingw安装程序,我的理解是它应该是32位的。通过一些谷歌搜索,我发现mingw-64是从mingw32分支出来的,而我肯定没有安装那个分支。Python肯定是32位的。 - cabraut
1个回答

1
在这个编译步骤中,输入文件将被编译为C++代码,并且symbol.cc中的函数将被赋予不兼容C语言的符号(名称混淆)。
“C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\site-packages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple.cc -o build\temp.win32-2.7\Release\simple.o”
在这个编译步骤中,输入文件将被编译为C代码,并且symbols.cc中的函数的符号应该是不同的。
“C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\sitepackages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple_wrap.c -o build\temp.win32-2.7\Release\simple_wrap.o”
解决问题的一种方法是添加swig_opts
setup(name='matt_simple_test', version='1.0', 
      ext_modules=[Extension('_simple', ['simple.cc', 'simple.i'],
                   swig_opts=["-c++"],
                   include_dirs = [numpy.get_include(),'.'])])

另一个选择是在simple.cc中使用extern "C"
extern "C" double dot(int n, double *a, int m, double *b)
...
extern "C" void create_list(int size, double *arr)
...

是的,将 swig_opts 添加到设置命令中就OK了!非常感谢! - cabraut

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接