linking for embedded python and C extensions

Fred Clift fred at cs.byu.edu
Fri Aug 25 17:59:55 EDT 2000


Ok -- a follow up to my recent posting about c->python->c integration.
Thanks to Warren P and to Mike Romberg who both advised me to look into
swig, which is a great tool that does about 4/5ths of what I want,
automatically.


So now I have what I guess is a linking question.  Let me explain in
detail what I'm doing and what is wrong:

I made a simple test C program that has a module (a.o) that I ran swig
on.

I make a FreeBSD shared lib with these options (roughly)

ld -shared  -fpic a.o setter_wrap.o \
/usr/local/lib/python1.5/config/libpython1.5.a -o settermodule.so

I can then run python and import setter and access funtions in it and it
works just fine.


Then I made a 'main.c' program that also links with a.o and calls
functions in it, invokes python and sends some simple test python code
to it...

This works just fine.

Then I want to, from within the python invoked by my c program, import
setter as above, and have it hopefully access the same data via calls to
the functions in a.o that it imports...

so I do something approximately like:

gcc $-D_THREAD_SAFE -pthread -o main main.o a.o \
-L/usr/local/lib/python1.5/config -lpython1.5  -lreadline  -lcrypt -lm


to make the executable 'main'


when I run main, I get


Fatal Python error: PyThreadState_Get: no current thread

Searching usenet archives for this error shows that this type of error
generlly happens when you have two versions (or perhaps two of the same)
libpythonX.Y.a dynamically loaded.  So, you end up with one copy in
memory as part of the executable main, and you can run  python code,
import modules (not my own of course) and do whatever as I need.  Except
when I import my C-based extension shared library, I get a second copy
of libpython1.5.a and things blow up.  At least this is my current
operating theory.


So, what should I be doing different to link?  how can I make this work?

I'm considering making a statically linked python with my module in it
but this would, at best, be very annoying.  It will slow down my
development a lot if I have to rebuild python everytime I want to change
the interface a little bit...


Is there anything else I can do?  ideas?


Fred


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list