This is the mail archive of the
mailing list for the Cygwin project.
Re: Global constructors in DLL ?
- To: "Colin Peters" <colin at fu dot is dot saga-u dot ac dot jp>
- Subject: Re: Global constructors in DLL ?
- From: Mumit Khan <khan at xraylith dot wisc dot edu>
- Date: Wed, 12 Aug 1998 11:59:03 -0500
- Cc: "Edwin Essenius" <edwin at bitbybit-is dot nl>, "GNU-win32" <gnu-win32 at cygnus dot com>, scox at cygnus dot com, cygwin32-developers at cygnus dot com
"Colin Peters" <email@example.com> writes:
> Edwin Essenius <firstname.lastname@example.org> wrote:
> >I am using cywin B19.2, with the sergey patches an trying to build a
> >C++ DLL. Except for the Global constructors all seems to work.
> >Global variables in the DLL are initialized, but the global constructors
> >are not called when it is declared in a DLL.
> If you have written or obtained your own startup code for the DLL (what the
> entry point is set to on the link line, or DllMain possibly) then you should
> check that you are calling __main in the startup code on DLL_PROCESS_ATTACH.
> __do_global_dtors should be called on DLL_PROCESS_DETACH. Also make sure to
> disable atexit inside the DLL (mainly for the call to __main) or the main
> process may attempt to call your destructors when it exits, which could be
> after the DLL has been detached.
Colin's right on the mark as to the cause of the global constructors not
working correctly. Here's the change to gcc sources that causes this
Mon Mar 30 13:11:05 1998 Stan Cox <email@example.com>
* libgcc2.c: (__main, __do_global_dtors, __do_global_ctors):
For __CYGWIN32__ use the versions in winsup/dcrt0.cc.
The problem is that the current default DLL startup in winsup calls
*its* definition of __do_global_ctors, which doesn't call GCC generated
C++ global initializers (cf: __CTOR__LIST__ and __DTOR_LIST__), and hence
your C++ globals aren't being initialized.
I get around it by using my own DLL startup, but that's a path I won't
recommend since you might be missing things that the winsup DLL startup
does beyond just initializing globals.
Stan, could you please take a look at this problem? The easiest way is to
simply copy the libgcc2.c version of DO_GLOBAL_CTORS_BODY (gbl-ctors.h)
into winsup's __do_global_ctors (and similarly for __do_global_dtors).
ps: I'm copying this message to cygwin32-developers list as well, since this
has the potential to be another "cygwin32 comes with broken C++" story.
For help on using this list (especially unsubscribing), send a message to
"firstname.lastname@example.org" with one line of text: "help".