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?