C++, ld & global object's constructors

C++, ld & global object's constructors

Post by Sebastien Foucault (Stagiaire O » Tue, 20 Aug 1996 04:00:00



I have a problem i am fed up with....

First of all here is the compiler i am using and
the OS i am working on :

Quote:> [noe] ~/Workin/PlugIn/DLL > g++ --version ; uname -a
> 2.5.8
> SunOS noe 5.4 generic sun4d sparc

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I a developping a plugin for netscape 3.0 : a plugin consists
in a shared library used by netscape in order to
enhance its capabilities. The library i am writing is supposed
to loaded by netscape : because i don't link netscape with
my dll, i suppose the libdl (with dlopen, dlsym functions)
is used (by netscape) to load my library...

The problem is that i HAVE (because of the existing code i am using)
to deal with  global objects in my library. And of course, the constructors are not
called...

I have read the g++ FAQ and especially the part entitled << Global
constructors aren't being called >>. However I have no problem with
global object when they are part of "normal" (ie static program)...
That's the reason why i think that the FAQ cannot apply to my problem
(i am maybe wrong ; if i am, i could try to patch my linker as
proposed in the FAQ).

The problem seems to be due to the linker (solaris ld). Indeed,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have no problem using GNU linker under Linux

Information :
~~~~~~~~~~~

I build  
- foo.so like this:
          g++ -shared -o foo.so foo1.o foo2.o ... fooN.o

- and fooY.o like this :
          g++ -fpic -c fooY.c

I would be happy not to upgrade my compiler with a more recent version...

Thank's in advance

--


--
+---------------------------------------+


+---------------------------------------+

 
 
 

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. Help! GNU Make 3.58 won't compile under 3005

3. Constructors of global objects in DSOs

4. vmlinuz/lilo grrr.argh!.die.die

5. Linking C and C++ code object files with 'ld'?

6. How come nobody likes XView?

7. ld error: Undefined symbol: global constructors keyed to TimeBase.ccJYfMec

8. Canon BJC 4000 or 4100 opinions?

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

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

11. Multiply defined C++ constructors in Shared Objects

12. Problem: shared object loading runs constructor of a static object, but static linkage does not.

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