Json: Duplicate symbols error happens while to_json/from_json method implemented inside entity definition header file

Created on 31 Mar 2017  ·  5Comments  ·  Source: nlohmann/json

Can't upload the project file, so I try my best to make this clear...

I implemented the to_json/from_json methods inside the entity definition header file. Suppose the file called A.h, and include this file inside the main.cpp source file. I have another source file called B.h, it does nothing but include A.h and define a class called B. When I compile the whole project, I got a duplicate symbols error.

1>ConsoleApplication10.obj : error LNK2005: "void __cdecl from_json(class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer> const &,class A &)" (?from_json@@YAXABV?$basic_json@Vmap@std@@Vvector@2@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@_N_J_KNVallocator@2@Uadl_serializer@nlohmann@@@nlohmann@@AAVA@@@Z) 已经在 B.obj 中定义 1>ConsoleApplication10.obj : error LNK2005: "void __cdecl to_json(class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer> &,class A const &)" (?to_json@@YAXAAV?$basic_json@Vmap@std@@Vvector@2@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@_N_J_KNVallocator@2@Uadl_serializer@nlohmann@@@nlohmann@@ABVA@@@Z) 已经在 B.obj 中定义 1>D:\Documents\Visual Studio 2015\Projects\ConsoleApplication10\Debug\ConsoleApplication10.exe : fatal error LNK1169: 找到一个或多个多重定义的符号

And I solved this by set to_json/from_json as static methods. But, according to the tutorial, I don't have to do this. I don't know if this is a document error, or I use this in a wrong way.

Sorry for my poor English...

A.h

#pragma once

#include "json.hpp"

using json = nlohmann::json;


class A
{};

void to_json(json& j, const A& val) {
}

void from_json(const json& j, A& val) {
}

B.h

#pragma once

#include "A.h"

class B
{
public:
    B();
    ~B();
};

B.cpp

#include "stdafx.h"
#include "B.h"


B::B()
{
}


B::~B()
{
}

main file

#include "stdafx.h"

#include "A.h"

using json = nlohmann::json;

int main()
{
    json _ = A();

    return 0;
}
question proposed fix

Most helpful comment

Hi, there's two solutions to that:

  • add inline before the function definition
  • implement from/to_json in a .cpp file

All 5 comments

Hi, there's two solutions to that:

  • add inline before the function definition
  • implement from/to_json in a .cpp file

Thanks for your answer. BTW, can it be a available solution to solve this problem by adding [static] before the function?

Honestly I don't know if static can cause trouble in this case, I almost never define static methods in headers.
I think you won't have multiple definitions though

OK, Thanks.

Is it a good idea to add a short note to the documentation about this?

Was this page helpful?
0 / 5 - 0 ratings