This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/790: Linking errors on attempts to access static members of classes and structs
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/790: Linking errors on attempts to access static members of classes and structs
- From: hoopak at hotmail dot com
- Date: 13 Nov 2000 22:00:02 -0000
- Reply-To: hoopak at hotmail dot com
>Number: 790
>Category: c++
>Synopsis: Linking errors on attempts to access static members of classes and structs
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Mon Nov 13 14:06:01 PST 2000
>Closed-Date:
>Last-Modified:
>Originator: Chad Simmons
>Release: gcc version 2.96 20000731 (Red Hat Linux 7.0)
>Organization:
>Environment:
Red Hat Linux version 7.0
GCC was installed via RPM from the standard Red Hat Distribution
>Description:
The linker generates an error when the code tries to access static members of structures and classes. I've double checked the C++ syntax for declaring and accessing them (because at first I just assumed I was doing it wrong) but all the C++ books I found said that I was using the static members properly.
>How-To-Repeat:
Here is the file I used to repeat the error
test.cpp
==========================================================
struct A
{
private:
static int struct_private;
public:
static int struct_public;
A() { struct_private++; }
~A() { struct_private--; }
};
class B
{
private:
static int class_private;
public:
static int class_public;
B() { class_private++; }
~B() { class_private--; }
};
static int global;
int main()
{
static int local;
A::struct_public = 1;
B::class_public = 1;
A testA;
B testB;
global = 1;
local =1;
return 0;
}
==========================================================
Compiler/Linker Output
**********************************************************
$ g++ -Wall test.cpp -o test --save-temps
test.o: In function `main':
test.o(.text+0x8): undefined reference to `A::struct_public'
test.o(.text+0x12): undefined reference to `B::class_public'
test.o: In function `A::A(void)':
test.o(.A::gnu.linkonce.t.(void)+0x8): undefined reference to `A::struct_private'
test.o: In function `A::~A(void)':
test.o(.gnu.linkonce.t._._1A+0xb): undefined reference to `A::struct_private'
test.o: In function `B::B(void)':
test.o(.B::gnu.linkonce.t.(void)+0x8): undefined reference to `B::class_private'
test.o: In function `B::~B(void)':
test.o(.gnu.linkonce.t._._1B+0xb): undefined reference to `B::class_private'
collect2: ld returned 1 exit status
$
**********************************************************
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: