繁体   English   中英

SWIG,mingw32,distutils的问题

[英]Problems with SWIG, mingw32, distutils

我一直在尝试在Windows 7下设置Python 2.7环境,以便我可以编译C ++扩展以在Python中使用。 由于我是新手,我在这里下载了一个简单的例子并逐字使用了这些文件。 我在路径中也有一个numpy.i文件。 我用mingw(最新版本)和swig(v.3.0.10)设置了我的计算机,我的Python版本是2.7.9。 我甚至使用这个环境来编译一个使用g ++的小型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 )从这里逐字逐句使用。

所以,问题是:有人可以指导我纠正这个错误吗?

在此编译步骤中,输入文件被编译为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)
...

暂无
暂无

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

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