hrr
hrr

Reputation: 1866

"static const int" causes linking error (undefined-reference)

I am baffled by the linker error when using the following code:

// static_const.cpp -- complete code
#include <vector>

struct Elem {
    static const int value = 0;
};

int main(int argc, char *argv[]) {
    std::vector<Elem> v(1);
    std::vector<Elem>::iterator it;

    it = v.begin();
    return it->value;
}

However, this fails when linking -- somehow it needs to have a symbol for the static const "value."

$ g++ static_const.cpp 
/tmp/ccZTyfe7.o: In function `main':
static_const.cpp:(.text+0x8e): undefined reference to `Elem::value'
collect2: ld returned 1 exit status

BTW, this compiles fine with -O1 or better; but it still fails for more complicated cases. I am using gcc version 4.4.4 20100726 (Red Hat 4.4.4-13).

Any ideas what might be wrong with my code?

Upvotes: 18

Views: 12190

Answers (6)

iammilind
iammilind

Reputation: 70058

In most of the compilers defining in-class static const <integral type> works fine. But some compilers like Android NDK, such in-class definitions results in linker errors. For such case, we may use the typed enums:

struct X
{
  enum : int64_t { VALUE = 100; }; // == static const int64_t VALUE = 100;
};

Upvotes: 2

Serge
Serge

Reputation: 1067

Also see this post: essentially, the problem is that somehow compiler ends up expanding your code into taking the address of Elem::value.

Upvotes: 2

karlphillip
karlphillip

Reputation: 93468

If you want to initialize it inside the struct, you can do it too:

struct Elem {
    static const int value = 0;
};

const int Elem::value;

Upvotes: 10

user439793
user439793

Reputation:

Why not just do this?

return Elem::value;

But the answer is that you are assigning a value in the declaration. This is supposed to work for basic types such as int, and is only required for complex types (i.e. classes, such as if you had a string instead of int). What I have found in practice is that this is hit or miss depending on what version of what compiler you are using. And, as you found out, which optimization level.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283793

static class members are generally supposed to be defined outside the class (declared inside, defined outside) in one compilation unit.

I don't remember how that interacts with inline initialization of const static integral members.

Upvotes: 2

jonsca
jonsca

Reputation: 10381

Try writing it as

struct Elem {
    static const int value;
};

const int Elem::value = 0;

etc

.

Upvotes: 5

Related Questions