使用SimpleJSON库(C+;+;)的多个定义 我用C++编写了一个程序,使用SimpleJSON作为我选择的JSON库,并遇到了一个奇怪的bug,下面的(相当简单的)文件在链接时生成了大量错误消息:

使用SimpleJSON库(C+;+;)的多个定义 我用C++编写了一个程序,使用SimpleJSON作为我选择的JSON库,并遇到了一个奇怪的bug,下面的(相当简单的)文件在链接时生成了大量错误消息:,c++,linker,simplejson,multiple-definition-error,C++,Linker,Simplejson,Multiple Definition Error,jsonobject.hpp: #ifndef _JSONOBJECT #define _JSONOBJECT #include "../../lib/simplejson/json.hpp" int foo(); #endif jsonobject.cpp: #include "jsonobject.hpp" int foo() { return 0; } main.cpp: #include "jsonobject.hpp" int main() { return

jsonobject.hpp:

#ifndef _JSONOBJECT
#define _JSONOBJECT

#include "../../lib/simplejson/json.hpp"

int foo();

#endif
jsonobject.cpp:

#include "jsonobject.hpp"

int foo()
{
   return 0;
}
main.cpp:

#include "jsonobject.hpp"

int main()
{
    return foo();
}
错误消息(我正在使用
g++*.cpp-std=c++11
进行编译):

/tmp/ccMoY6Vl.o:在函数“json::Array()”中:
main.cpp:(.text+0x1cd):'json::Array()的多个定义
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x1cd):首先在这里定义
/tmp/ccMoY6Vl.o:在函数“json::Object()”中:
main.cpp:(.text+0x23e):'json::Object()的多个定义
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x23e):首先在这里定义

/tmp/ccMoY6Vl.o:在函数`json::operator中,您使用的SimpleJSON库存在问题。快速查看json.hpp可以确认它在头文件中定义了几个命名空间作用域非模板函数,而没有将它们标记为
inline
。这使得不可能包含来自多个翻译单元的标题,这严重降低了它的使用方式


这已经有报道。看起来至少有一个人尝试并提交了对个人分支的修复,但尚未将其拉到主分支。您可能可以从该分支获取代码来试用。

今天晚些时候我将试用,谢谢您的提示。更新:2021该问题仍然存在。所以我宁愿寻找替代品。在它的当前状态下,它是不可用的。
/tmp/ccMoY6Vl.o: In function `json::Array()':
main.cpp:(.text+0x1cd): multiple definition of `json::Array()'
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x1cd): first defined here
/tmp/ccMoY6Vl.o: In function `json::Object()':
main.cpp:(.text+0x23e): multiple definition of `json::Object()'
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x23e): first defined here
/tmp/ccMoY6Vl.o: In function `json::operator<<(std::ostream&, json::JSON const&)':
main.cpp:(.text+0x2af): multiple definition of `json::operator<<(std::ostream&, json::JSON const&)'
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x2af): first defined here
/tmp/ccMoY6Vl.o: In function `json::JSON::Load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
main.cpp:(.text+0x1a0c): multiple definition of `json::JSON::Load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccjd4YF7.o:jsonobject.cpp:(.text+0x1a0c): first defined here
collect2: error: ld returned 1 exit status