Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker error LNK2005 when compiling (x64) json-3.10.0.zip with Visual Studio 2019 16.11.1 #2941

Closed
2 of 5 tasks
Andreas-Schniertshauer opened this issue Aug 18, 2021 · 5 comments · Fixed by #2945
Closed
2 of 5 tasks
Assignees
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Andreas-Schniertshauer
Copy link

What is the issue you have?

Linker error LNK2005:

unit-regression2.obj : error LNK2005: "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * sax_no_exception::error_string" (?error_string@sax_no_exception@@2PEAV?$basic_string@DU?$char_traits@D@std@@V?$allo
cator@D@2@@std@@EA) already defined in unit-disabled_exceptions.obj [build\json\test\json_unit.vcxproj]
build\json\test\Release\json_unit.exe : fatal error LNK1169: one or more multiply defined symbols found [build\json\test\json_unit.vcxproj]

Can you provide a small but working solution / fix?

Add a namespace in both files json\test\src\unit-disabled_exceptions.cpp and json\test\src\unit-regression2.cpp:

namespace
{
  class sax_no_exception : public nlohmann::detail::json_sax_dom_parser<json>
  {
  ...
  };
  std::string* sax_no_exception::error_string = nullptr;
}  // namespace

What is the expected behavior?

No link error

And what is the actual behavior instead?

LNK2005 error

Which compiler and operating system are you using?

  • Compiler: Visual Studio 2019 16.11.1 Professional
  • Operating system: Windows 10 21H1

Which version of the library did you use?

  • latest release version 3.10.0 (zip file)
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
@nlohmann
Copy link
Owner

Very strange. We are using these versions in the CI without problems:

image

Can you share the build settings? What are you building? I am confused that unit-disabled_exceptions.cpp and unit-regression2.cpp should affect each other.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Aug 18, 2021
@nlohmann
Copy link
Owner

nlohmann commented Aug 18, 2021

Alright, I can reproduce what you're doing. You're building the binary json_unit which links all tests. The unit tests are documented to be build with

$ mkdir build
$ cd build
$ cmake .. -DJSON_BuildTests=On
$ cmake --build .
$ ctest --output-on-failure

which still works. I'll check if it makes sense to fix the target json_unit or to remove it altogether - it's an artifact from earlier times when the test suite was not yet built with CMake.

@nlohmann nlohmann added state: please discuss please discuss the issue or vote for your favorite option and removed kind: bug platform: visual studio related to MSVC state: needs more info the author of the issue needs to provide more details labels Aug 18, 2021
@Andreas-Schniertshauer
Copy link
Author

I am building in the x64 Developer Command Prompt of Visual Studio 2019 16.11.1 with cmake 3.20.21032501-MSVC_2 (Visual Studio) and msbuild 16.11.0.36601 with a lot of parameters passed to cmake, but you can reproduce it very easily with this command:

cmake ..\..\json .

I use normally these flags for cmake from json:
-D JSON_Install:BOOL=OFF -D JSON_ImplicitConversions:BOOL=OFF -D JSON_BuildTests:BOOL=ON

@nlohmann
Copy link
Owner

But what do you build then?

@nlohmann
Copy link
Owner

nlohmann commented Aug 18, 2021

(The binary json_unit is marked EXCLUDE_FROM_ALL, so I don't think it was built with the test suite.)

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: please discuss please discuss the issue or vote for your favorite option labels Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants