This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: C++ static integer class constants...


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 <iostream>
#include <utility>

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<unsigned char, unsigned char> method(bool redundant) const {
        int offset = (redundant ? B : A);
        
        return std::pair<unsigned char,
                         unsigned char>(sram[offset], sram[offset + 1]);
    }
};

int main(int, char **) {
    foo f;
    std::pair<unsigned char, unsigned char> p1(f.method(false));
    std::pair<unsigned char, unsigned char> 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




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]