From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26936 invoked by alias); 14 Aug 2008 11:49:19 -0000 Received: (qmail 26927 invoked by uid 22791); 14 Aug 2008 11:49:18 -0000 X-Spam-Check-By: sourceware.org Received: from exprod6og117.obsmtp.com (HELO exprod6og117.obsmtp.com) (64.18.1.39) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 14 Aug 2008 11:48:35 +0000 Received: from source ([192.150.11.134]) by exprod6ob117.postini.com ([64.18.5.12]) with SMTP; Thu, 14 Aug 2008 04:48:33 PDT Received: from inner-relay-1.corp.adobe.com ([153.32.1.51]) by outbound-smtp-1.corp.adobe.com (8.12.10/8.12.10) with ESMTP id m7EBipG3007675; Thu, 14 Aug 2008 04:44:51 -0700 (PDT) Received: from apacmail.pac.adobe.com (apacmail.pac.adobe.com [130.248.36.99]) by inner-relay-1.corp.adobe.com (8.12.10/8.12.10) with ESMTP id m7EBmViq001223; Thu, 14 Aug 2008 04:48:32 -0700 (PDT) Received: from namailgen.corp.adobe.com ([10.8.192.91]) by apacmail.pac.adobe.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 14 Aug 2008 20:48:31 +0900 Received: from 10.7.234.24 ([10.7.234.24]) by namailgen.corp.adobe.com ([10.8.192.91]) via Exchange Front-End Server namail.corp.adobe.com ([10.8.189.97]) with Microsoft Exchange Server HTTP-DAV ; Thu, 14 Aug 2008 11:48:29 +0000 User-Agent: Microsoft-Entourage/12.12.0.080729 Date: Thu, 14 Aug 2008 12:21:00 -0000 Subject: Re: External variables in shared library constructor code From: Eljay Love-Jensen To: smokyboy , GCC-help Message-ID: In-Reply-To: <18976389.post@talk.nabble.com> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-08/txt/msg00141.txt.bz2 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