extern const char*const SOME_CONSTANT给我链接器错误
extern const char* const SOME_CONSTANT giving me linker errors
我想在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";
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 创建带有const char*的对象时使用std::make_unique时发生链接错误
- 尝试将const char *转换为lptstr时的链接器错误
- 链接器错误(未定义的引用)与“静态 constexpr const char*”和完美转发
- 将构造函数与 const char * 作为参数链接会产生意外的结果
- C++中const的内部链接,但我得到了重复的符号
- c++多定义链接错误与.h中声明的const
- 外部const char* const指针或外部const char数组[]导致链接错误
- const extern c数组链接器多重定义
- extern const char*const SOME_CONSTANT给我链接器错误
- 使用 llvm::Function::d ump(),链接器可以"undefined reference to `llvm::Value::dump() const'"