Skip to content

How to resolve " undefined reference to `std::__throw_bad_cast()'" #1111

Closed

Description

@TridentTD
  • What is the issue you have?

I can compile well on ESP8266's xtensa_gcc compiler version5.1.0,
however when I want to receive string by dump() function .
It occurs some errors. How to resolve it?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

First I try this code

json j = {  {"pi", 3.141}, {"happy", true}, {"name", "testtest"}, 
                    {"nothing", nullptr}, { "answer", { {"everything", 42} } },
                    {"list", {1, 0, 2}}, { "object", { {"currency", "USD"}, {"value", 42.99} } } };

Serial.println((float) j["pi"],3);     // print to Serial Monitor

The result can print well on Serial Monitor --> 3.141
However when I want to dump like the following.

std::string dump_string =  j.dump();

It occurs error like this

libstdc++.a(ostream-inst.o): In function `std::ostream& std::ostream::_M_insert<long>(long)':
libstdc++-v3/include/ostream:544: undefined reference to `std::__throw_bad_cast()'
libstdc++.a(wlocale-inst.o): In function `std::ctype<wchar_t> const& std::use_facet<std::ctype<wchar_t> >(std::locale const&)':
libstdc++-v3/include/bits/locale_facets_nonio.tcc:1173: undefined reference to `std::__throw_bad_cast()'
...

How to resolve "undefined reference to `std::__throw_bad_cast()' " error ?

Thank you.

Activity

  1. nlohmann commented on May 27, 2018

    @nlohmann
    Owner

    As wchar_t are mentioned: the return type of j.dump() is std::string - I am not sure about Serial.println, but if it expects wchar_t, this may be a problem. Quick googling also found this issue: https://stackoverflow.com/questions/22263732/undefined-reference-to-stdbad-castbad-cast

    Can you compile and execute the test suite?

  2. TridentTD commented on May 27, 2018

    @TridentTD
    Author

    My Serial.println() already know std::string
    Serial_println_std_string

    But when call j.dump() , it occurs std::__throw_bad_cast() error.
    dump_call

    dump_error

    How can I change about THROW or anything like that to null String when it can't casting?

  3. nlohmann commented on May 27, 2018

    @nlohmann
    Owner
  4. TridentTD commented on May 28, 2018

    @TridentTD
    Author

    Thank you.

    It seems to be an issue by c++ compiler config / flags.
    If I take "noexcept" flag , is there any problem for the json library ?

  5. nlohmann commented on May 28, 2018

    @nlohmann
    Owner

    You mean -fno-exceptions? That works, but please note that without exceptions, the library will abort in case of an error.

  6. TridentTD commented on May 28, 2018

    @TridentTD
    Author

    I've already tried , but std::__throw_bad_cast() errors still occur.

  7. nlohmann commented on May 28, 2018

    @nlohmann
    Owner

    According to esp8266/Arduino#3358 the problem could be the usage of string streams. They are used in three locations in the code - each only in case of an error. You could try to change these locations by replacing ss.str() with "" and removing all std::stringstream occurrences. If this helps, we may get rid of string streams altogether.

  8. nlohmann commented on Jun 1, 2018

    @nlohmann
    Owner

    @TridentTD Could you check the most recent develop version? With #1117, we removed stringstreams.

  9. TridentTD commented on Jun 1, 2018

    @TridentTD
    Author

    By the most recent develop version,
    I used the simgle include for testing.

    Result :
    by_dev_version

  10. nlohmann commented on Jun 1, 2018

    @nlohmann
    Owner

    Then I have no idea how to proceed.

  11. TridentTD commented on Jun 5, 2018

    @TridentTD
    Author

    Do you mind to use Arduino's String (WString.h/ WString.cpp)
    instread of std::string for Arduino user ?

    Thank you.

  12. nlohmann commented on Jun 18, 2018

    @nlohmann
    Owner

    @TridentTD You can template the string type for the JSON library yourself.

  13. nlohmann commented on Jun 28, 2018

    @nlohmann
    Owner

    @TridentTD Do you need any further assistance?

  14. TridentTD commented on Jul 1, 2018

    @TridentTD
    Author

    @nlohmann
    I have alread ported Arduino-Core's WString library for using on any other C++ platform.
    https://github.com/TridentTD/Arduino_WString/

    But my knowledge about C++ template is very little, and about std::string or std::__throw_bad_cast() too,
    So I don't know how to replace std:string and std::__throw_bad_cast() by Arduino' WString library.

  15. pboettch commented on Jul 1, 2018

    @pboettch
    Contributor

    On an embedded platform where there is no standard library and no standard crt0 you need to provide your own implemention of basic "system" functions. That's why it is a link error and not a compile error.

    Arduino's libraries should provide it but it seems they don't.

    Just put a function like this in your application:

    namespace std {
    
    void __throw_bad_cast(void)
    {
        // print error and stop execution
    }
    
    } // namespace std

    Not sure about the prototype though.

  16. nlohmann commented on Jul 9, 2018

    @nlohmann
    Owner

    @TridentTD Any news on this?

  17. nlohmann commented on Jul 11, 2018

    @nlohmann
    Owner
  18. nlohmann commented on Jul 21, 2018

    @nlohmann
    Owner

    @TridentTD Any news on this?

  19. added
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions