C++链接器抱怨char*有多个定义,但没有std::string
C++ Linker complains multiple definition of char* but not std::string
在一个大项目中,我有一个.h文件,它在一个命名空间中定义了很多常量。引入const-char*常量会导致链接器出现错误,抱怨有多个定义。
例如h
#include <string>
namespace Dv
{
const int MAX = 10;
const std::string NAME = "bobo";
const char* NAME2 = "fred"; // <-- ERROR: multiple definition of `Dv::NAME2'
}
exA.cpp
#include "ex.h"
void aFunction() { printf("this is aFunctionn"); }
exB.cpp
#include "ex.h"
void aFunction(void);
int main(int argc, char **argv)
{
aFunction();
return 0;
}
编译和链接
g++ -c exA.cpp
g++ -c exB.cpp
g++ exA.o exB.o -o ex
exB.o:(.data+0x0): multiple definition of `Dv::NAME2'
exA.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
如果在.h文件中包含NAME2,则会发生链接器错误。为什么?
如果MAX也是POD(如char[]?),为什么链接器不抱怨多个int MAX定义?(或者多个std::string NAME定义?)
我不明白在这种情况下char[]有什么特别之处。
感谢
根据C++标准(3.5程序和链接)
3具有命名空间作用域(3.3.6)的名称具有内部链接,如果它是的名称
--显式声明为const或constexpr且既没有显式声明extern也没有先前声明的非易失性变量声明具有外部链接;或
和
4未命名的命名空间或直接或间接声明的命名空间在未命名的命名空间中具有内部链接所有其他名称空间具有外部链接具有命名空间作用域的名称未被赋予内部链接。以上链接与封闭命名空间(如果它是变量的名称);或
在这个名称空间定义中
namespace Dv
{
const int MAX = 10;
const std::string NAME = "bobo";
const char* NAME2 = "fred"; // <-- ERROR: multiple definition of `Dv::NAME2'
}
(唯一的)变量NAME2
不是常数,因此具有外部链接。
您可以将其设置为具有内部链接来定义它,例如使用关键字static。例如
namespace Dv
{
const int MAX = 10;
const std::string NAME = "bobo";
static const char* NAME2 = "fred";
}
或者可以将其定义为恒定
namespace Dv
{
const int MAX = 10;
const std::string NAME = "bobo";
const char * const NAME2 = "fred";
}
相关文章:
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 自定义堆栈上 std::string 数据的输出
- 将 std::string 转换为 wchar_t* 的类型定义
- Cpp 未定义引用 'Apache:: thrift:: transport:: TSocket:: TSocket (std:: string const&, int)'
- 从<string>向量<int>中定义的索引处的向量中删除字符串
- 对"cv::String::allocate(unsigned long)"的未定义引用
- C 与stat lib中的std :: string相关的未定义符号
- 对'PreconditionViolatedException::PreconditionViolatedException(std::string const&)'的未定义引用
- 链接webrtc本机时出错,因为未定义对具有std::string的方法的引用
- 如何为 Product* getProductFromID(std::string)编写方法定义;.
- 如果将键定义为std :: string,则存储在REDIS中的POD结构的验证将失败
- C :与宏定义的字符串Concat Constant String
- 对'std::string Helper::ToString<int>(int const&)'的未定义引用
- 在函数中定义的 std::string 保留其先前的值
- 错误未定义对"BP::D evice::Create(std::string const&)"的引用
- 创建自定义" string "(结构)副本
- 用户自定义string::reserve()
- 从返回类型为用户定义string类的函数返回std::string时出错