Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern const char* const SOME_CONSTANT giving me linker errors

I want to provide a string constant in an API like so:

extern const char* const SOME_CONSTANT;

But if I define it in my static library source file as

const char* const SOME_CONSTANT = "test";

I'm getting linker errors when linking against that library and using SOME_CONSTANT:

Error 1 error LNK2001: unresolved external symbol "char const * const SOME_CONSTANT" (?SOME_CONSTANT@@3QBDB)

Removing the pointer const-ness (second const keyword) from both the extern const char* const declaration and the definition makes it work. How can I export it with pointer const-ness?

like image 214
AndiDog Avatar asked Feb 20 '13 10:02

AndiDog


1 Answers

The problem could be that the extern declaration is not visible in the source file defining the constant. Try repeating the declaration above the definition, like this:

extern const char* const SOME_CONSTANT;  //make sure name has external linkage
const char* const SOME_CONSTANT = "test";  //define the constant
like image 53
Angew is no longer proud of SO Avatar answered Sep 27 '22 19:09

Angew is no longer proud of SO