extern const char*const SOME_CONSTANT给我链接器错误

extern const char* const SOME_CONSTANT giving me linker errors

本文关键字:const 链接 错误 char SOME extern CONSTANT      更新时间:2023-10-16

我想在API中提供一个字符串常量,比如:

extern const char* const SOME_CONSTANT;

但是如果我在静态库源文件中将其定义为

const char* const SOME_CONSTANT = "test";

当链接到该库并使用SOME_CONSTANT:时,我会遇到链接器错误

错误1错误LNK2001:未解析的外部符号"char const*const SOME_CONSTANT"(?SOME_COConstANT@@3QBDB)

extern const char* const声明和定义中删除指针constness(第二个const关键字)可以使其工作。如何导出带有指针常量的

问题可能是extern声明在定义常量的源文件中不可见。尝试重复定义上面的声明,如下所示:

extern const char* const SOME_CONSTANT;  //make sure name has external linkage
const char* const SOME_CONSTANT = "test";  //define the constant

很可能您忘记将头包含在实现文件中

无论如何,将关键字extern添加到定义中

如果没有extern声明,它具有内部链接,因此对链接器不可见

接受答案的解决方案可以在一个步骤中完成。

您可以将extern说明符添加到contants的定义中。

extern const char* const SOME_CONSTANT = "test";