【发布时间】:2018-04-18 11:24:51
【问题描述】:
我正在使用 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) 的错?
【问题讨论】:
标签: c++ linker simplejson multiple-definition-error