Feb 15, 2012

Swig compiling issues Mac Os Lion

One problem that happened to me, was the fact that working under a virtualenv with an specific python version as one can expect, works with ONLY that python version.
So if you try to import a module compiled with one version different with the one you are running, will no work.
The exception message:

Fatal Python error: Interpreter not initialized (version mismatch?)
Abort trap: 6

How we can solve this issue?
First, knowing which python version are we running and of course, compile the module to THAT version.
Below are some examples of how to compile a module to 2.6 or 2.7 (which is Mac Os Lion python default version)

To use python default version just type the following:

$ swig -python example.i
$ cc -c `python-config --cflags` example.c example_wrap.c
$ cc -bundle `python-config --ldflags` example.o example_wrap.o -o _example.so

If you run:

$ python-config --ldflags

the output is:

-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -ldl -framework CoreFoundation -lpython2.7 -u _PyMac_Error /System/Library/Frameworks/Python.framework/Versions/2.7/Python
$ python-config --cflags
-I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64

In case you want you use a different version of python (let's say 2.6) do the following:

$ swig -python example.i
$ cc -c `python2.6-config --cflags` example.c example_wrap.c
$ cc -bundle `python2.6-config --ldflags` example.o example_wrap.o -o _example.so

If you run

$ python2.6-config --ldflags

the output is:

-L/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config -ldl -framework CoreFoundation -lpython2.6

$ python2.6-config --cflags
-I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 

To see other installed python versions:

$ ls /usr/bin/

1 comment:

Anonymous said...

Thanks! After searching through pages of info with nothing working this got SWIG using all the right Python libraries and my project's working.