Possible Duplicate:
Why does const imply internal linkage in C++, when it doesn’t in C?
What is external linkage and internal linkage in C++
I have a two C files that I’m trying to compile to an executable. One file contains only a single declaration as follows (simplifed).
const char *foo[2] = {"thing1", "thing2"};
The second c file does this
extern const char *foo[2];
main()
{
//Code that does stuff with foo
}
When compiling I get a linker error that foo is an unresolved external symbol. I’m assuming the compiler is optimizing out foo. Any ideas here?
There’s nothing wrong with your declarations. The code should compile and link as is, assuming you add explicit
intas the return type of yourmain. The only explanation for the linker error I can come up with is that you are forgetting to supply all required object files to the linker.The answers that attempt to explain this issue through the fact that in C++
constobjects have internal linkage are misleading and irrelevant. The above objectfoois not aconstobject.