我正在尝试使用distutils模块在Linux上为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 interface file
/* 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
有没有人有关于如何管理这个任务的想法?
pexports
版本已经过时了。当前版本来自 MinGW.org,是 pexports-0.46(我在两年半前发布的)。这个版本可以作为 Linux 主机交叉编译工具进行构建(这是我更新它的主要动机),尽管在我的 64 位 Linux 主机上,它偶尔会出现段错误(我只在尝试从 Win98 的MSVCRT.DLL
中转储导出函数时注意到这一点),由于将数据转换为 64 位指针时出现了错误;(我仍在努力追踪这个 bug)。 - Keith Marshallmsi
文件中捆绑的libpython27.a
不适用于mingw32?它是以某种方式格式错误吗?还是它是为不同的架构而设计的? - Keith Marshallar
档案;这个预构建的libpython27.a是一个(格式不正确的)导入库,(我们通常希望Visual Studio创建为.lib
文件)。 对于MinGW和Visual Studio,对象格式相同(COFF),在这种情况下,所有导入存根都是有效的COFF对象。 毛病在于一对头/尾对象(指定DLL关联); 这些_也应该是COFF对象文件,但在这种情况下它们不是...它们是没有特定可识别格式的原始数据(从GNU binutils的角度来看)。 - Keith Marshall