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

Error on the Mac: Undefined symbols for architecture x86_64 #285

Closed
a-x- opened this issue Jul 24, 2016 · 9 comments
Closed

Error on the Mac: Undefined symbols for architecture x86_64 #285

a-x- opened this issue Jul 24, 2016 · 9 comments
Labels
solution: invalid the issue is not related to the library

Comments

@a-x-
Copy link

a-x- commented Jul 24, 2016

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

@nlohmann
Copy link
Owner

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.

@a-x-
Copy link
Author

a-x- commented Jul 24, 2016

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

@nlohmann
Copy link
Owner

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.

@a-x-
Copy link
Author

a-x- commented Jul 25, 2016

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

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Jul 25, 2016
@WikiWikiWang
Copy link

WikiWikiWang commented Nov 30, 2017

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

@nlohmann
Copy link
Owner

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

@wcochran
Copy link

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

@nicolasjulioflores
Copy link

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 !

@matricali
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

6 participants