extern const char* const SOME_CONSTANT giving me linker errors(extern const char* const SOME_CONSTANT 给我链接器错误)
问题描述
我想在 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:
错误 1 错误 LNK2001:无法解析的外部符号char const * const SOME_CONSTANT"(?SOME_CONSTANT@@3QBDB)
Error 1 error LNK2001: unresolved external symbol "char const * const SOME_CONSTANT" (?SOME_CONSTANT@@3QBDB)
从 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 给我链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:extern const char* const SOME_CONSTANT 给我链接器错误
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
