This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/13356] New: undefined reference to static members of struct temp instances
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 8 Dec 2003 17:10:03 -0000
- Subject: [Bug c++/13356] New: undefined reference to static members of struct temp instances
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Consider the following code:
--------------------------------------------------
#include <iostream>
using namespace std;
struct traits
{
static const char a_char = 1;
static const int an_int = 1;
};
int
main ()
{
cerr << traits ().a_char << endl;
cerr << traits ().an_int << endl;
}
--------------------------------------------------
this code is rejecting by G++ 3.2, 3.3, and 3.4 (but for different reasons)
but accepted by como and icc:
| /tmp % for i in g++-3.2 g++-3.3 g++-3.4 icc como
| do
| echo $i
| $i foo1.cc
| echo $?
| echo
| for> done
| g++-3.2
| /tmp/ccdAJN3r.o(.text+0x40): In function `main':
| : undefined reference to `traits::an_int'
| collect2: ld returned 1 exit status
| 1
|
| g++-3.3
| /tmp/ccvudqJ0.o(.text+0x35): In function `main':
| : undefined reference to `traits::an_int'
| collect2: ld returned 1 exit status
| 1
|
| g++-3.4
| /tmp/cc38E7e7.o(.text+0x13): In function `main':
| : undefined reference to `traits::a_char'
| /tmp/cc38E7e7.o(.text+0x38): In function `main':
| : undefined reference to `traits::an_int'
| collect2: ld a retourné 1 code d'état d'exécution
| 1
|
| icc
| 0
|
| como
| Comeau C/C++ 4.3.3 (Sep 29 2003 14:52:48) for RedHat_LINUX_INTEL_ELF_Beta9
| Copyright 1988-2003 Comeau Computing. All rights reserved.
| MODE:non-strict warnings C++
|
| 0
Note that, weirdly enough, g++-3.2 and 3.3 produce different behavior
depending upon the type of the class member!
Passing -Wall produces no additional information.
Environment:
System: Linux nostromo 2.4.22 #1 SMP mer sep 17 19:49:48 CEST 2003 i686 GNU/Linux
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ./configure
How-To-Repeat:
Just try.
------- Additional Comments From akim at lrde dot epita dot fr 2003-12-08 17:09 -------
Fix:
The workaround is quite easy: the class member ought to be called as a
class member, and not as a object member from a temporary (as is
customary for traits):
| /tmp % cat foo2.cc
| #include <iostream>
|
| using namespace std;
|
| struct traits
| {
| static const char a_char = 1;
| static const int an_int = 1;
| };
|
| int
| main ()
| {
| cerr << traits ::a_char << endl;
| cerr << traits ::an_int << endl;
| }
| /tmp % for i in g++-3.2 g++-3.3 g++-3.4 icc como
| do
| echo $i
| $i foo2.cc
| echo $?
| echo
| done
| g++-3.2
| 0
|
| g++-3.3
| 0
|
| g++-3.4
| 0
|
| icc
| 0
|
| como
| Comeau C/C++ 4.3.3 (Sep 29 2003 14:52:48) for RedHat_LINUX_INTEL_ELF_Beta9
| Copyright 1988-2003 Comeau Computing. All rights reserved.
| MODE:non-strict warnings C++
|
| 0
--
Summary: undefined reference to static members of struct temp
instances
Product: gcc
Version: 3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: akim at lrde dot epita dot fr
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13356