From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3976 invoked by alias); 16 Oct 2005 16:42:53 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 3960 invoked by uid 22791); 16 Oct 2005 16:42:51 -0000 Received: from sccimhc91.asp.att.net (HELO sccimhc91.asp.att.net) (63.240.76.165) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Sun, 16 Oct 2005 16:42:51 +0000 Received: from xaivrxp (12-210-217-111.client.insightbb.com[12.210.217.111]) by sccimhc91.asp.att.net (sccimhc91) with SMTP id <20051016164249i91001cfeje>; Sun, 16 Oct 2005 16:42:49 +0000 From: "John Ratliff" To: Subject: RE: C++ static integer class constants... Date: Sun, 16 Oct 2005 16:42:00 -0000 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit In-Reply-To: <3518719F06577C4F85DA618E3C37AB9101389214@nimbus.ott.qnx.com> X-SW-Source: 2005-10/txt/msg00086.txt.bz2 Message-ID: <20051016164200.SXtKlzuuD3wFbp8uCZTs90WoTJLFjxhvdIl7U7D4_MA@z> I am replying to the wrong message. Sorry, I was not subscribed until just now and don't have the correct one to reply to. I'll quote it from the web mailing archive. >> On Sun, Oct 16, 2005 at 02:35:52AM -0500, John Ratliff wrote: >> class foo { >> public: >> static const int X = 5; >> static const int Y = 10; >> static const int Z = 15; >> }; >> >> However, when I went to Linux, where I have g++ 3.3.5 (or possibly 3.3.6, >> but I think it's 3.3.5), the linker would complain about unresolved >> symbols. >I compiled a simple code using such constants here with no problems >using GCC 3.3.4 and 3.4.3. Sorry. I should know better than to post a useless example. >> If I were to define the static variable in my implementation file, the >> linker would find the variables and go on its merry way. In other words, >> I could solve the problem by doing this: >> >> const int foo::X; >> const int foo::Y; >> const int foo::Z; >This is usually only needed when, e. g., you take the address of a >constant: >const int *x = &foo::X; >Do you have a minimal sample which does not work? I do now. #include #include class foo { private: char sram[0x2000]; public: static const int A = 0x8; static const int B = 0x1FF8; foo() { sram[8] = 1; sram[9] = 2; sram[0x1FF8] = 3; sram[0x1FF9] = 4; } std::pair method(bool redundant) const { int offset = (redundant ? B : A); return std::pair(sram[offset], sram[offset + 1]); } }; int main(int, char **) { foo f; std::pair p1(f.method(false)); std::pair p2(f.method(true)); std::cout << "p1 = (" << (int)p1.first << "," << (int)p1.second << ")\n"; std::cout << "p2 = (" << (int)p2.first << "," << (int)p2.second << ")\n"; return 0; } Compiles and links just fine under mingw/g++ 3.4.2 and not at all under Linux g++/3.3.3. I compiled the test example under SourceForge's compile farm linux host 2 (I think they run Fedora Core 4) The non-working g++ gives this for g++ --version ---------------------------------------------------- g++ (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7) Copyright (C) 2003 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ---------------------------------------------------- When I do $ g++ -W -Wall temp.cc /tmp/ccSsK02S.o(.gnu.linkonce.t._ZNK3foo6methodEb+0x13): In function `foo::method(bool) const': : undefined reference to `foo::B' /tmp/ccSsK02S.o(.gnu.linkonce.t._ZNK3foo6methodEb+0x1d): In function `foo::method(bool) const': : undefined reference to `foo::A' collect2: ld returned 1 exit status ---------------------------------------------------- $ g++ --version g++.exe (GCC) 3.4.2 (mingw-special) Copyright (C) 2004 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ---------------------------------------------------- For my working mingw/g++. I don't see anywhere that I'm taking the address of A or B. I don't see why they should need storage space here. Am I doing something wrong in my program? Thanks, --John Ratliff