mostrich
mostrich

Reputation: 3

C++: undefined reference to const pointer when declaring as extern

I get a compiler error when programming the following:

    //file1.cpp
    int a,b,c,d;
    int *const pa[4] = {&a, &b, &c, &d};

    //file2.cpp
    extern int *const pa[4];

when compiling it drops the error in file2.cpp:

undefined reference to `pa'

How to define that pointer array with constant pointers and use it in different source files?

Best regards :-)

Upvotes: 0

Views: 557

Answers (1)

VLL
VLL

Reputation: 10165

A const variable has internal linkage by default. To get external linkage, add extern to the definition:

extern int *const pa[4] = {&a, &b, &c, &d};

Upvotes: 1

Related Questions