Json: Error on the Mac: Undefined symbols for architecture x86_64

Created on 25 Jul 2016  路  9Comments  路  Source: nlohmann/json

I installed json with brew (brew tap nlohmann/json && brew install nlohmann_json).
then i wrote hello world:

#include "json.hpp"
using json = nlohmann::json;
    // create object from string literal
    auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");

finnaly i ran clang: clang -std=c++11 -O3 compare-vectors.cpp -o compare-vectors

Undefined symbols for architecture x86_64:
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      void std::__1::vector<nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator>, std::__1::allocator<nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator> > >::__push_back_slow_path<nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator> >(nlohmann::basic_json<std::__1::map, std::__1::vector, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, bool, long long, unsigned long long, double, std::__1::allocator>&&) in compare-vectors-480e6b.o
...
      ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix

invalid

Most helpful comment

I know this is an old issue, but just for resolution: using g++/clang++ in place of gcc/clang solves the issue.

All 9 comments

This is very strange, because all code is checked by Travis, including Xcode 7.3 (see https://travis-ci.org/nlohmann/json/jobs/146965482), so I currently cannot reproduce the error.

Could you please post the complete file compare-vectors.cpp. Furthermore, which compiler options are you using, because Homebrew requires to set an include directory which is missing in your example.

i simplified file:

< compare-vectors.cpp
#include "json.hpp"
int main(void) {
    using json = nlohmann::json;
    // create object from string literal
    auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
}

clang -std=c++11 -O3 compare-vectors.cpp -o compare-vectors

Could you try to execute brew test nlohmann_json?

The error message seems similar to issue reported here: http://stackoverflow.com/questions/19637164/c-linking-error-after-upgrading-to-mac-os-x-10-9-xcode-5-0-1 - you may want to try to add -stdlib=libc++ to the compiler flags.

brew test nlohmann_json passed correctly.

i tried both -stdlib=libc++ and -stdlib=libstdc++.

i use xcode.app now, which uses some sort of magic for running clang underhood

it's not ideal but i can live with it

I know this is an old issue, but just for resolution: using g++/clang++ in place of gcc/clang solves the issue.

Google led me to this thread once again, and replacing clang with clang++ solved the issue again :)

Yes, you need to using clang++ for name mangling purposes.

Switching from gcc to g++ to compile solved this issue for me (for a non-JSON issue, but similar issue). Thank you so much @WikiWikiWang !

Thank you @WikiWikiWang, switching to g++ instead of gcc solves my issue. THANKS a lot!

Was this page helpful?
0 / 5 - 0 ratings