【发布时间】:2015-09-02 19:01:40
【问题描述】:
我正在尝试使用 distutils 模块在 Linux for Windows (mingw32) 上交叉编译一个简单的 SWIG Python 扩展。
最终目标是为某些库编译 Python 包装器并能够在 Windows 上使用它。显然我从最基本的例子开始,不幸的是它失败了。
这是我正在使用的文件:
example.c
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
example.i - SWIG 接口文件
/* File : example.i */
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}
setup.py
# setup.py
import distutils
from distutils.core import setup, Extension
setup(name = "SWIG example",
version = "1.0",
ext_modules = [Extension("_example", ["example.i","example.c"])])
为了使用本机 (Linux) gcc 编译器进行编译,我正在调用:
python setup.py build
一切都像魅力一样!不幸的是,在尝试指定 Windows 目标时:
python setup.py build --compiler=mingw32
我收到错误说 gcc 无法识别 -mdll 开关:
running build
running build_ext
building '_example' extension
swigging example.i to example_wrap.c
swig -python -o example_wrap.c example.i
creating build
creating build/temp.linux-x86_64-2.7
gcc -mdll -O -Wall -I/home/jojek/anaconda/include/python2.7 -c example_wrap.c -o build/temp.linux-x86_64-2.7/example_wrap.o
gcc: error: unrecognized command line option ‘-mdll’
error: command 'gcc' failed with exit status 1
很公平,这很有意义,因为工具链无效。我确保在我的机器上安装了mingw32。通过调用dpkg -L mingw32,我知道编译器位于/usr/bin/i586-mingw32msvc-gcc。
我的下一步是使用编译器的实际路径覆盖 CC 环境变量。当我尝试再次编译它时,我收到以下错误,缺少 sys/select.h 头文件:
running build
running build_ext
building '_example' extension
swigging example.i to example_wrap.c
swig -python -o example_wrap.c example.i
creating build
creating build/temp.linux-x86_64-2.7
/usr/bin/i586-mingw32msvc-gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/jojek/anaconda/include/python2.7 -c example_wrap.c -o build/temp.linux-x86_64-2.7/example_wrap.o
example_wrap.c:1: warning: -fPIC ignored for target (all code is position independent)
In file included from /home/jojek/anaconda/include/python2.7/Python.h:58,
from example_wrap.c:125:
/home/jojek/anaconda/include/python2.7/pyport.h:351:24: error: sys/select.h: No such file or directory
error: command '/usr/bin/i586-mingw32msvc-gcc' failed with exit status 1
有人知道如何管理该任务吗?
【问题讨论】:
标签: python gcc mingw swig distutils