delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/11/17/04:19:45

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <20031117091932.55655.qmail@web21403.mail.yahoo.com>
Date: Mon, 17 Nov 2003 20:19:32 +1100 (EST)
From: =?iso-8859-1?q?Danny=20Smith?= <danny_r_smith_2001 AT yahoo DOT co DOT nz>
Subject: RE[2]: Exporting const variables from DLLs (GCC bug?)
To: cygwin AT cygwin DOT com
MIME-Version: 1.0

 --- Danny Smith <danny_r_smith_2001 AT yahoo DOT co DOT nz> wrote: > Jon Foster wrote:
> 
> > Given this source code: 
> > extern const int meaning_of_life __declspec(dllexport);
> > const int meaning_of_life __declspec(dllexport) = 42;
> > 
> > 
> > GCC complains: 
> > $ c++ -g -O2 -c test.cxx -o test.o
> > test.cxx:2: error: external linkage required for symbol 'const int
> >    meaning_of_life' because of 'dllexport' attribute
> >
> 
> 
> It is a bug in gcc.  The above code compiles okay with C, but
> strangely, not C++.  For some reason, g++ does not immediately
> mark the the definition of global constants as public when they
> are defined after a prior declaration.  I have a fix that I will
> submit to gcc-patches after reg-testing
> 

On second thought, I don't think  I will.
In c++, const variables at namespace scope have internal linkage unless
declared with explicit extern (as you have done).  But I've just run this
testcase on gcc and VC and I get essentially the same error reports:

/* const.cpp */
/* 1: TREE_PUBLIC is set when dllexport attribute is handled. */
 __declspec(dllexport) int bar = 42;  // OK

/* 2: This is OK too. TREE_PUBLIC is set, because of 'extern' */
extern  __declspec(dllexport) const int baz;
const int baz = 42;  //OK 

/* 3: TREE_PUBLIC _not_  set. Nor should it be. consts are local 
   by default in c++ */
 __declspec(dllexport) const int foo = 42;  // ERROR

/* 4: TREE_PUBLIC _not_  set.  */
extern  __declspec(dllexport) const int faz;
__declspec (dllexport) const int faz = 42;  // ERROR

int fun()
{ return foo + faz; }


I thought that case 4 should be okay because of the 'extern' on declaration,
but if GCC wants to maintain consistency with native compiler, 4 should report
an error as well. The error mesage that VC emits is: error C2201: 'faz' : must
have external linkage in order to be exported/imported

Danny

http://personals.yahoo.com.au - Yahoo! Personals
New people, new possibilities. FREE for a limited time.

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019