SWIG 在 AIX 上崩溃(使用 python,可能还有其他所有 SWIG 支持)

发布于 2024-08-11 08:00:16 字数 2282 浏览 3 评论 0原文

SWIG 在 AIX 上可以轻松编译和安装。不幸的是,一个简单的 SWIG hello world (也可以编译 - 但不那么容易)会因分段而崩溃错误或非法指令(取决于编译/链接器过程的一些细节)。 gcc 和 xlc(IBM c 编译器)都会发生这种情况。我只尝试了本机 AIX 链接器 ld,因为我的系统上没有安装同名 GNU ld。

文件:example.c

 #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
    return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

文件:example.i

%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

Makefile 片段:

swig -python example.i
xlc -q64 -c example.c example_wrap.c -I/your-python-path/include/python2.5/
ld -G -b64 -berok -bnoentry -bexpall -brtl example.o example_wrap.o -o _example.so

链接器步骤是有问题的。如果您按照教程上的示例进行操作,则应该这样做

ld -bshared example.o example_wrap.o -o _example.so #the b is not a typo, but a different syntax in AIX vd GNU ld

不幸的是,由于多种原因,这不起作用。我相信 IBM/AIX 和开源社区对于“共享库”的含义有着截然不同的想法。从 AIX 本机链接器获得的最常见的共享对象 (so) 中根本没有任何符号(实际上大小不到 1kB)。 出现一长串未解析的符号,如下所示):

ld: 0711-317 ERROR: Undefined symbol: PyType_Type

从链接器获得损坏的输出也很容易(在这种情况下,链接时会 wiki/RTFM" rel="nofollow noreferrer">应该这样做,很明显,解决方案是使用各种链接器选项,-berok-bnoentry、<代码>-bexpall、<代码>-brtl、<代码>-bshared、<代码>-bM:SRE、<代码>-bexpfull。事实上,可以找到一些组合来创建非空的 .so 库,而不会产生错误。上面的 Makefile 片段中报告了其中一种组合(还有其他组合)。不幸的是,它们都在以下两种模式之一中失败了!

$ python -c "import example"
Illegal instruction (core dumped)

$ python -c "import example"
Segmentation fault (core dumped)

使用 gcc,或不同版本的 python(我们有 7 个!),32 位或 64 位不会改变任何内容:您可以找到一个“好的”链接选项,但它在运行时崩溃。怎么解决这个问题呢?

SWIG compiles and install easily on AIX. Unfortunately, a simple SWIG hello world (which also compiles - but not so easily) crashes with Segmentation Fault or Illegal Instruction (depending on some details of the compilation/linker process). This happens with both gcc and xlc (IBM c compiler). I tried only the native AIX linker ld, because the homonyms GNU ld was not installed on my system.

File: example.c

 #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
    return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(<ime);
     return ctime(<ime);
 }

File: example.i

%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

Makefile snippet:

swig -python example.i
xlc -q64 -c example.c example_wrap.c -I/your-python-path/include/python2.5/
ld -G -b64 -berok -bnoentry -bexpall -brtl example.o example_wrap.o -o _example.so

The linker step is the problematic one. If you follow the examples on the tutorial, you should do

ld -bshared example.o example_wrap.o -o _example.so #the b is not a typo, but a different syntax in AIX vd GNU ld

Unfortunately this does not work for several reasons. I believe that IBM/AIX and the Open Source communities have quite a different thoughts on what "shared library" means. The most common shared objects (so) that you get from the AIX native linker have no symbols at all in them (and are in fact less than 1kB in size). It's also pretty easy to get broken output from the linker (in such a case a quite long list of unresolved symbols like the following appears while linking):

ld: 0711-317 ERROR: Undefined symbol: PyType_Type

Doing what one is supposed to do, it seems clear that the solution is hacking with the various linker options, -berok, -bnoentry, -bexpall, -brtl, -bshared, -bM:SRE, -bexpfull. In fact, it is possible to find some combinations which create a non-empty .so library, without generating errors. One of these combinations is reported in the Makefile snippet above (there are others). Unfortunately, all of them fail in one of the following two modes!

$ python -c "import example"
Illegal instruction (core dumped)

or

$ python -c "import example"
Segmentation fault (core dumped)

Using the gcc, or a different version of python (we have 7!) either 32 bit or 64 bit does not change anything: you can find a "good" link option, but it crashes at runtime. How to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2024-08-18 08:00:16

这篇文章并没有直接帮助我,但再次为我指明了正确的方向 - 但情况不同了,我正在构建一个嵌入式Python。请参阅 http://docs.python.org/extending/embedding.html#linking -要求

>>> import distutils.sysconfig
>>> distutils.sysconfig.get_config_var("LINKFORSHARED")
'-Wl,-bE:Modules/python.exp -lld'

This post didn't help me directly, but again pointed me in the right direction - but then the situation is different, I am building an embedded python. See http://docs.python.org/extending/embedding.html#linking-requirements

>>> import distutils.sysconfig
>>> distutils.sysconfig.get_config_var("LINKFORSHARED")
'-Wl,-bE:Modules/python.exp -lld'
忘年祭陌 2024-08-18 08:00:16

这不是一个实际的问题,而是关于我如何解决问题的报告(请参阅这里为什么我要这样做)。实际上我自己无法解决这个问题,但这要归功于 另一个人。我在这里重写它,因为他太具体了(AIX 5.1 with perl and C++,我在寻找其他东西时偶然发现了他!当我搜索这个时,我根本找不到他的答案问题!我希望这篇文章能被更多人找到!
我的问题是在使用 python 和 C 的 AIX 5.3 上。我相信 AIX 上的每个 SWIG 安装都是常见的(因此我没有标记 python 和 C)。我会尽快联系开发人员,以便他们首先修复帮助。

好吧,修复方法很简单,就是使用不同的链接线,具体如下:

ld -G -bI:/your-python-path/lib/python2.5/config/python.exp -bnoentry -bexpall -lC -lc -ldl example.o example_wrap.o -o _example.so

关键是 exp 文件,您应该为您的语言/安装找到该文件:

find /your-python-or-perl-or-other-language-path/ -name *exp

希望这会有所帮助!

This is not an actual question, but a report on how I fixed my problem (see here why I'm doing this). And actually I wasn't able to solve it myself, but it was thanks to this other guy. I am rewriting it here, because he was too specific (AIX 5.1 with perl and C++, and I found him serendipitously, while I was searching for something else! I wasn't able to find his answer at all when I was searching for this problem! I hope this post will be more findable to others!
My problem is on AIX 5.3 with python and C. I believe that it is common to every SWIG installation on AIX (thus I didn't tag python and C). I'll contact the developers soon so they might fix the help in the first place.

Well, the fix is simply to use a different link line, in particular as follows:

ld -G -bI:/your-python-path/lib/python2.5/config/python.exp -bnoentry -bexpall -lC -lc -ldl example.o example_wrap.o -o _example.so

The key is the exp file, which you should find yourself for your language/installation:

find /your-python-or-perl-or-other-language-path/ -name *exp

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文