[英]Problems with SWIG, mingw32, distutils
I have been trying to set up a Python 2.7 environment under Windows 7 so that I can compile a C++ extension for use in Python. 我一直在尝试在Windows 7下设置Python 2.7环境,以便我可以编译C ++扩展以在Python中使用。 Since I am new to this, I have downloaded a simple example here and have used the files verbatim.
由于我是新手,我在这里下载了一个简单的例子,并逐字使用了这些文件。 I also have a numpy.i file in the path.
我在路径中也有一个numpy.i文件。 I have set up my computer with mingw (latest version) and swig (v. 3.0.10), and my Python version is 2.7.9.
我用mingw(最新版本)和swig(v.3.0.10)设置了我的计算机,我的Python版本是2.7.9。 I have even used this environment to compile a small C++ program using g++ with no problem.
我甚至使用这个环境来编译一个使用g ++的小型C ++程序没有问题。
But when trying to build the "simple" Python extension referenced above, I always get the following output, indicating failure (I have included the command that I issued in the Windows cmd.exe window as the first line below): 但是当我尝试构建上面引用的“简单”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
I have a terrible feeling that I'm missing something very simple here, but I've managed to successfully compile these identical files in a separate Cygwin environment with no issues (and yes, it is desirable for me to be able to do this in a non-Cygwin environment). 我有一种可怕的感觉,我在这里遗漏了一些非常简单的东西,但我已经成功地在一个单独的Cygwin环境中成功编译了这些相同的文件而没有任何问题(是的,我希望能够在一个非Cygwin环境)。
I don't want to choke this question with too much code, but, for reference, here are the files simple.i
and setup.py
that I'm using. 我不想用太多代码来扼杀这个问题,但是,作为参考,这里是我正在使用的文件
simple.i
和setup.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(),'.'])])
If other code is necessary, I am happy to post them, but again, the other files ( simple.cc
and simple.h
) are used verbatim from here . 如果需要其他代码,我很乐意发布它们,但同样,其他文件(
simple.cc
和simple.h
)从这里逐字逐句使用。
So, the question is: can anyone guide me to correct this error? 所以,问题是:有人可以指导我纠正这个错误吗?
In this compilation step the input file is compiled as C++ code and the functions in symbol.cc will be given symbols that are incompatible with C (name mangling). 在此编译步骤中,输入文件被编译为C ++代码,symbol.cc中的函数将被赋予与C(名称修改)不兼容的符号。
C:\\MinGW\\bin\\gcc.exe -mdll -O -Wall -IC:\\Python27\\lib\\site-packages\\numpy\\core\\include -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
-IC:\\ Python27 \\ include -IC:\\ Python27 \\ PC -c simple.cc -o build \\ temp.win32-2.7 \\ Release \\ simple.o
In this compilation step the input file is compiled as C code and the symbols for the functions in symbols.cc are expected to be different. 在此编译步骤中,输入文件被编译为C代码,而symbols.cc中函数的符号应该是不同的。
C:\\MinGW\\bin\\gcc.exe -mdll -O -Wall -IC:\\Python27\\lib\\sitepackages\\numpy\\core\\include -I.
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
-IC:\\ Python27 \\ include -IC:\\ Python27 \\ PC -c simple_wrap.c -o build \\ temp.win32-2.7 \\ Release \\ simple_wrap.o
One way to fix the problem is to add swig_opts
解决问题的一种方法是添加
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(),'.'])])
Another option is to use extern "C"
in simple.cc 另一种选择是在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.