linking for embedded python and C extensions (Solved)

Fred Clift fred at cs.byu.edu
Mon Aug 28 15:56:09 EDT 2000


In article <8o6q8f$mpl$1 at nnrp1.deja.com>,
  Fred Clift <fred at cs.byu.edu> wrote:
>
> I made a simple test C program that has a module (a.o) that I ran swig
> on.
>
> 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...
>...
> when I run main, I get
>
> Fatal Python error: PyThreadState_Get: no current thread
...
> So, what should I be doing different to link?  how can I make this
work?
>


Ok -- to follow up to my own posting -- Mike Romberg sent me email
helping me figure out what was wrong -- on the final link, I needed to
pass the linker the -E option so that dependencies of my shared library
could be met by objects in the executable itself  -- from the C compiler
you do a -Wl,-E  for gnu's ld.  (see my simple makefile below....)

Then he suggested rather than building the shared library that I just
make a .o from setter_wrap.c and statically link it in to my program,
rather than having to have python dynamically load it (and hence,
needing to have my PYTHONPATH environment var set...)  Doing  this
however has one snag which Mike pointed out -- you have to call the
module initialization function (the last funct that swig puts into your
wrap.c file) right after your Py_Initialize.

So, I have a main.c that does

        Py_Initialize();
        initsetter();
        PyRun_SimpleString("import setter");
        PyRun_SimpleString("setter.doMyStuff()");

and all works great.

Thanks for all who sent me suggestions and help.

By the way, replacing the first PyRun_SimpleString("import setter");
with PyImport_ImportModule("setter"); doesn't work.  I thought the two
statements are roughly equivalent.  Anyone know why they act different?


Here is a copy of the makefile I use, in hopes that it might help others
in the future :)


---------------------------------------------------------------------
OPT =-I/usr/local/include/python1.5 -DHAVE_CONFIG_H -D_THREAD_SAFE
-pthread -pipe

main: clean Makefile a.o main.o setter_wrap.o
        gcc $(OPT)  -Wl,-E -o main main.o a.o setter_wrap.o
/usr/local/lib/python1.5/config/libpython1.5.a  -lreadline  -lcrypt -lm

a.o: a.c a.h
        gcc $(OPT) -c a.c

setter_wrap.o: setter_wrap.c
        gcc $(OPT) -fpic -c setter_wrap.c

main.o: main.c
        gcc $(OPT) -c main.c

clean:
        rm -f *.o ;rm -f settermodule.so; rm -f *.core

----------------------------------------------------------------


Fred


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



More information about the Python-list mailing list