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

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

Closed
TridentTD opened this issue May 27, 2018 · 18 comments
Closed

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

TridentTD opened this issue May 27, 2018 · 18 comments
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@TridentTD
Copy link

  • 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.

@nlohmann
Copy link
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?

@TridentTD
Copy link
Author

TridentTD commented May 27, 2018

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?

@nlohmann
Copy link
Owner

@TridentTD
Copy link
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 ?

@nlohmann
Copy link
Owner

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

@TridentTD
Copy link
Author

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

@nlohmann
Copy link
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.

@nlohmann
Copy link
Owner

nlohmann commented Jun 1, 2018

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

@TridentTD
Copy link
Author

TridentTD commented Jun 1, 2018

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

Result :
by_dev_version

@nlohmann
Copy link
Owner

nlohmann commented Jun 1, 2018

Then I have no idea how to proceed.

@TridentTD
Copy link
Author

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

Thank you.

@nlohmann
Copy link
Owner

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

@nlohmann
Copy link
Owner

@TridentTD Do you need any further assistance?

@TridentTD
Copy link
Author

TridentTD commented Jul 1, 2018

@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.

@pboettch
Copy link
Contributor

pboettch commented Jul 1, 2018

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.

@nlohmann
Copy link
Owner

nlohmann commented Jul 9, 2018

@TridentTD Any news on this?

@nlohmann
Copy link
Owner

@TridentTD Does esp8266/Arduino#3358 (comment) work for you?

@nlohmann
Copy link
Owner

@TridentTD Any news on this?

@nlohmann nlohmann added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 21, 2018
@stale stale bot closed this as completed Jul 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants