使用 SimpleJSON 库 (C++) 的多个定义
Multiple definitions with SimpleJSON library (C++)
我正在使用 SimpleJSON 作为我选择的 JSON 库用 C++ 编写一个程序,并且 运行 变成了一个奇怪的错误,其中如下(非常简单)文件在链接时生成大量错误消息:
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: 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
我是不是哪里做错了,还是 SimpleJSON 库 (Here it is on GitHub) 的错?
是的,您使用的 SimpleJSON 库有问题。快速查看 json.hpp 确认它在 header 文件中定义了几个 namespace-scope non-template 函数,但没有标记它们 inline
。这使得无法包含来自多个翻译单元的 header,这严重减少了它的使用方式。
这已经被报道过 on SimpleJSON's git issue tracker。看起来至少有一个人已经尝试并提交了对个人分支的修复,但还没有被拉到主分支。您也许可以从该分支获取代码来试用它。
作者库已存档。它现在是只读的。
所以我无法在原始问题板上创建拉取请求或评论。
我找到了一个分为两个文件的解决方案,如下所示:
https://github.com/andakkino/SimpleJSON
我已经用此内容创建了一个 json.cpp 文件
#include <json.hpp>
using namespace json;
JSON JSON::Load( const string &str ) {
size_t offset = 0;
return std::move( parse_next( str, offset ) );
}
在 .hpp 文件中,我在一些函数中添加了“内联”,显然,删除了加载函数
我正在使用 SimpleJSON 作为我选择的 JSON 库用 C++ 编写一个程序,并且 运行 变成了一个奇怪的错误,其中如下(非常简单)文件在链接时生成大量错误消息:
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: 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
我是不是哪里做错了,还是 SimpleJSON 库 (Here it is on GitHub) 的错?
是的,您使用的 SimpleJSON 库有问题。快速查看 json.hpp 确认它在 header 文件中定义了几个 namespace-scope non-template 函数,但没有标记它们 inline
。这使得无法包含来自多个翻译单元的 header,这严重减少了它的使用方式。
这已经被报道过 on SimpleJSON's git issue tracker。看起来至少有一个人已经尝试并提交了对个人分支的修复,但还没有被拉到主分支。您也许可以从该分支获取代码来试用它。
作者库已存档。它现在是只读的。 所以我无法在原始问题板上创建拉取请求或评论。
我找到了一个分为两个文件的解决方案,如下所示: https://github.com/andakkino/SimpleJSON
我已经用此内容创建了一个 json.cpp 文件
#include <json.hpp>
using namespace json;
JSON JSON::Load( const string &str ) {
size_t offset = 0;
return std::move( parse_next( str, offset ) );
}
在 .hpp 文件中,我在一些函数中添加了“内联”,显然,删除了加载函数