delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/24/08:18:44

From: pavenis AT lanet DOT lv
To: Tasci Synx <nospam AT puckbell DOT net>, djgpp AT delorie DOT com
Date: Mon, 24 Jul 2000 14:17:30 +0200
MIME-Version: 1.0
Subject: Re: Enabling Static Data in Templates
Message-ID: <397C4FFA.12305.5F312@localhost>
In-reply-to: <397BC997.9C504DD9@puckbell.net>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 23 Jul 2000, at 21:44, Tasci Synx wrote:

> This might be a problem with djgpp, so I wanted to ask here.
> 
> The Ansi C++ standard gives an example of a static data member in a
> templated class:
> ---------------------------------------------------------
> template<class T> class X {
> 	static T s;
> };
> template<class T> T X<T>::s = 0;
> ---------------------------------------------------------
> However, upon putting such a thing in a header file, I received a linker
> error.
> 
> test.o: In function `foo<int>::blech(void)':
> test.cpp:15: undefined reference to `foo<int>::bar'
> collect2: ld returned 1 exit status
> 
> Here is the test code I compiled:
> 
> ---------------------------------------------------------
> // in test.h
> #include <iostream>
> using std::cout;
> 
> template <class Type>
> struct foo
> {
> 	static Type bar;
> 	static void blech(void);
> };
> 
> template <class Type> Type foo<Type>::bar = 33;
> 
> template <class Type> inline void foo<Type>::blech(void)
> {
> 	cout << "Pass the bar:" << bar << endl;
> }
> ---------------------------------------------------------
> ---------------------------------------------------------
> // in test.cpp
> 
> #include "test.h"
> 
> void main(void)
> {
> 	foo<int> test;
> 	test.blech();
> }
> ---------------------------------------------------------
> 
> Is this a problem specific to djgpp?  Is there a switch I can use to
> make sure my static templated data members are created properly?
> 


Known problem. 

If You are using binutils-2.10 (bnu210b.zip) and latest update of 
DJGPP port of gcc-2.95.2 then You can try gcc command line option -
mbnu210:

	gcc -mbnu210 -O2 test.cpp -lstdcxx -o test.exe

Perhaps it will be default sometimes, but not now...

Andris

- Raw text -


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