This is quoted from the "1 Sep 1998" EGCS G++ FAQ which can be found on This FAQ is no longer maintained by Cygnus since the EGCS project and the GNU gcc project merged. This information is provided for historical purposes and may or may not be out of date at some point in the future.

How do I build shared libraries with g++?

   For gcc-2.7.0 and later, building C++ shared libraries should work
fine on supported platforms (HPUX 9+, IRIX 5+, DEC UNIX (formerly
OSF/1), SGI/IRIX, AIX, SunOS 4, Linux/ELF and all targets using
SVR4-style ELF shared libraries).  There are two separate issues:
building libg++ as a shared library, and making your own shared
libraries.  For libg++ it is simply a matter of giving the
`--enable-shared' option to the configure program.  When compiling your
own code for shared libraries you generally must use the `-fPIC' flag
to get position-independent code.

   If your shared library contains global or static objects with
constructors, then make sure to use `gcc -shared', not `ld', to create
the shared library.  This will make sure that any processor-specific
magic needed to execute the constructors is included.

   In theory, constructors for objects in your shared library should be
called when the library is opened (by dlopen or equivalent).  This does
not work on some platforms (e.g. SunOS4; it does work on Solaris and
ELF systems such as Linux): on the broken platforms, the constructors
are not called correctly.

   David Nilsen has suggested the following workaround:

   The thing to realize is that if you link your dynamic module with the
`-shared' flag, the collect program nicely groups all the static
ctors/dtors for you into a list and sets up a function that will call
them (Note: this means that this trick won't work if you use the GNU
linker without collect (*note use GNU linker?::.).

   The magic is knowing these function names.  Currently, they're

     _GLOBAL__DI   <-- calls all module constructors
     _GLOBAL__DD   <-- calls all module destructors

   [ possibly the leading underscore will differ between platforms:
jbuck ]

   Therefore, if you make a wrapper around dlopen that looks up the
symbol `_GLOBAL__DI' (or `__GLOBAL__DI' on SunOS4 machines), and calls
it, you'll simulate getting the constructors called.

   You also need to set up the destructors to be called as well, so you
need to put a wrapper around dlclose, which will call the `_GLOBAL__DD'
function in the module when/if it's unloaded.

   Lastly, to get things 100% correct, you need to set up the
destructors to also be called if the module is not unloaded, but the
main program exits.  I do this by registering a single function with
`atexit()' that calls all the destructors left in dynamically loaded

   Check the file `README.SHLIB' from the libg++ distribution for more
about making and using shared libraries.

   A patch is needed to build shared versions of version 2.7.2 of libg++
and libstdc++ on the HP-PA architecture.  You can find the patch at

Zachary C. Miller
Last modified: Tue Sep 25 14:46:55 CDT 2001