问题描述
我想在API中提供一个字符串常量,如下所示:
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";
针对该库进行链接并使用SOME_CONSTANT时,出现链接器错误:
I'm getting linker errors when linking against that library and using SOME_CONSTANT:
从extern const char* const
声明和定义中删除指针的const-ness(第二个const关键字)使其起作用.如何使用指针常量导出?
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?
推荐答案
问题可能是extern
声明在定义常量的源文件中不可见.尝试在定义上方重复声明,如下所示:
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
这篇关于extern const char * const SOME_CONSTANT给我链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!