From: Eljay Love-Jensen <eljay@adobe.com>
To: smokyboy <smokyboy0@lycos.com>, GCC-help <gcc-help@gcc.gnu.org>
Subject: Re: External variables in shared library constructor code
Date: Thu, 14 Aug 2008 12:21:00 -0000 [thread overview]
Message-ID: <C4C985B3.7D24%eljay@adobe.com> (raw)
In-Reply-To: <18976389.post@talk.nabble.com>
Hi smokyboy,
There are no guarantees as to the order of initialization in the C/C++
standards.
Some compilers provide initialization ordering extensions, but those are
usually within a package (such as a DLL), and not across packages (DLLs).
One solution is to hide the external variable within a global function call:
// C++
extern int& GlobalIntValue();
// C
extern int* GlobalIntValue();
That way, the function call can guarantee the variable is initialized on
demand, and that the global variable is only initialized once.
The routine will look like this, in C++:
int& GlobalIntValue()
{
static int foo = 77;
return foo;
}
And in C:
int* GlobalIntValue()
{
static int foo = 77;
return &foo;
}
HTH,
--Eljay
prev parent reply other threads:[~2008-08-14 11:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-14 7:16 smokyboy
2008-08-14 12:21 ` Eljay Love-Jensen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=C4C985B3.7D24%eljay@adobe.com \
--to=eljay@adobe.com \
--cc=gcc-help@gcc.gnu.org \
--cc=smokyboy0@lycos.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).