我一直试图在Windows7下设置Python2.7环境,这样我就可以编译一个C++扩展,以便在Python中使用。由于我是新手,所以我下载了一个简单的示例这里并逐字使用了这些文件。我在路径中还有一个numpy.i文件。我已经用mingw (最新版本)和swig (v.3.0.10)设置了我的计算机,我的Python版本是2.7.9。我甚至使用这个环境编译了一个使用g++编写的小型g++程序,没有问题。
但是,当试图构建上面引用的“简单”Python扩展时,我总是得到以下输出,表示失败(我已经将我在Windows窗口中发出的命令包含在下面的第一行):
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.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(),'.'])])如果需要其他代码,我很乐意发布它们,但同样,其他文件(simple.cc和simple.h)是从这里逐字使用的。
所以,问题是:有人能引导我纠正这个错误吗?
发布于 2016-07-14 01:57:17
在这个编译步骤中,输入文件被编译为C++代码,symbol.cc中的函数将被赋予与C (name mangling)不兼容的符号。
-IC:\Python27\lib\site-packages\numpy\core\include C:\MinGW\bin\gcc.exe -mdll -O -Wall -I.-IC:\Python27\include -IC:\Python27\PC -c simple.cc -o构建\tem.win32-2.7\Release\simple.o
在这个编译步骤中,输入文件被编译为C代码,symbols.cc中函数的符号应该是不同的。
-IC:\Python27\lib\sitepackages\numpy\core\include C:\MinGW\bin\gcc.exe -mdll -O -Wall -I.-IC:\Python27\include -IC:\Python27\PC -c simple_wrap.c -o构建\tem.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(),'.'])])另一种选择是在extern "C"中使用simple.cc
extern "C" double dot(int n, double *a, int m, double *b)
...
extern "C" void create_list(int size, double *arr)
...https://stackoverflow.com/questions/38311483
复制相似问题