Q: dynamic loading of C++ code to Python --- how to call global constructors?

Q: dynamic loading of C++ code to Python --- how to call global constructors?

Post by Paul Buny » Tue, 10 Nov 1998 04:00:00



Hi, everyone!

Sorry, for cross-posting, but really I hope someone in these groups
will be able to help me with my problem.

I am trying to extend Python (v. 1.5.1 on RedHat 5.1 Linux system)
with functions to interface to a (relatively large) C++
program. Everything works as described in docs, but, naturally, when I
load a shared library constructors for global objects are not called.

quote from Doc/ext.tex:
        It is possible to write extension modules in \Cpp{}.  Some
        restrictions apply.  If the main program (the Python interpreter)
        is compiled and linked by the C compiler, global or static
        objects with constructors cannot be used.

I tried to recompile python from RedHat 5.1 SRPM with CC=g++ but it
did not compile. Of course I can try to get rid of all the global
variables, but are there any intermediate (non-portable) solutions?

What g++ does to call global constructors? Is there a function (Info
mentions __main, but it did not work for me) which calls them, which I
can just put it in moduleinit()?

Thank you in advance for any info,

Paul

--
  ("`-''-/").___..--''"`-._   UNIX *is* user-friendly, he is just very
   `6_ 6  )   `-.  (     ).`-.__.`) picky about who his friends are...
   (_Y_.)'  ._   )  `._ `. ``-..-'      Paul Bunyk, Research Scientist
 _..`--'_..-_/  /--'_.' ,'art by           (and part-time UN*X sysadm)
(il),-''  (li),'  ((!.-' F. Lee http://pbunyk.physics.sunysb.edu/~paul

 
 
 

Q: dynamic loading of C++ code to Python --- how to call global constructors?

Post by Jack Jans » Wed, 11 Nov 1998 04:00:00



>Hi, everyone!
>Sorry, for cross-posting, but really I hope someone in these groups
>will be able to help me with my problem.
>I am trying to extend Python (v. 1.5.1 on RedHat 5.1 Linux system)
>with functions to interface to a (relatively large) C++
>program. Everything works as described in docs, but, naturally, when I
>load a shared library constructors for global objects are not called.
>quote from Doc/ext.tex:
>    It is possible to write extension modules in \Cpp{}.  Some
>    restrictions apply.  If the main program (the Python interpreter)
>    is compiled and linked by the C compiler, global or static
>    objects with constructors cannot be used.

It should definitely be possible to call the constructors, but this
needs machine-dependent code in importdl.c.

How this works on most platforms is that the loader (or compiler)
creates some glue code in the shared library, and this glue code is
exported in a standard way. Often it will be exported as a standard
symbol, sometimes it will be tucked away in a special header field in
the shared library format. All importdl.c would have to do is call
this routine. The question is, of course, who is going to write this
code...
--
--
Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++

http://www.cwi.nl/~jack | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm

 
 
 

1. Q: constructor not called for global object in solaris shared lib

This smells like an FAQ; but nothing poped up on a search.
 A global object is defined in a file (C++) :

                 Bus tmpbus;

This file is compiled and becomes partof shared library (.so) :

                 CC -c foo.cpp
                 ld -G -olibfoo.so foo.o

Another object is  linked to this shared library :

                 CC -o a.out main.cpp -L. -lfoo

Now, when we execute a.out, we find that the constructor for the object
tmpbus is not getting called.  If instead of creating a shared library out
of foo.o, it is directly linked with main.o, the contructor is called
properly.  Are we missing anything ?

We are using SUNWsproc C++ ver 4.1 and running solaris 2.x.  I do not
get to read the news group till the end of the day.  So, if you are
responding,

Thank you.

Subbu

2. keyboard spoof for solaris

3. KDE-Problems on Solaris: Global constructors not called!!

4. Newbee cgi bin questions

5. C++, ld & global object's constructors

6. french adb kbd map

7. Global constructors in ELF so libs NOT CALLED!

8. UNINSTALLING GRUB FROM REDHAT 7.3

9. calling constructors - C++/Shared Object/Solaris/NSAPI

10. Static constructors not called for C++ shared library

11. static constructor from shared library not getting called on aix when loaded by Apache Web Server

12. Bug: Dynamic Linking and global C++ objects

13. AIX shared libs with g++: global constructors not executed