extern const char* const SOME_CONSTANT giving me linker errors

前端 未结 2 984
迷失自我
迷失自我 2020-12-19 01:33

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 sour

相关标签:
2条回答
  • 2020-12-19 02:02

    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
    
    0 讨论(0)
  • 2020-12-19 02:04

    most probably you forgot to include your header in your implementation file

    anyway, add the keyword extern to the definition

    without an extern declaration it has internal linkage and is thus not visible to the linker

    0 讨论(0)
提交回复
热议问题