You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to resolve " undefined reference to `std::__throw_bad_cast()'" #1111
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?
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 ?
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.
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.
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:
Activity
nlohmann commentedon May 27, 2018
As
wchar_tare mentioned: the return type ofj.dump()isstd::string- I am not sure aboutSerial.println, but if it expectswchar_t, this may be a problem. Quick googling also found this issue: https://stackoverflow.com/questions/22263732/undefined-reference-to-stdbad-castbad-castCan you compile and execute the test suite?
TridentTD commentedon May 27, 2018
My Serial.println() already know std::string

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

How can I change about THROW or anything like that to null String when it can't casting?
nlohmann commentedon May 27, 2018
This seems to be an issue with the compiler.
Maybe related:
TridentTD commentedon May 28, 2018
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 commentedon May 28, 2018
You mean
-fno-exceptions? That works, but please note that without exceptions, the library will abort in case of an error.TridentTD commentedon May 28, 2018
I've already tried , but std::__throw_bad_cast() errors still occur.
nlohmann commentedon May 28, 2018
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 allstd::stringstreamoccurrences. If this helps, we may get rid of string streams altogether.nlohmann commentedon Jun 1, 2018
@TridentTD Could you check the most recent
developversion? With #1117, we removed stringstreams.TridentTD commentedon Jun 1, 2018
By the most recent develop version,
I used the simgle include for testing.
Result :

nlohmann commentedon Jun 1, 2018
Then I have no idea how to proceed.
TridentTD commentedon Jun 5, 2018
Do you mind to use Arduino's String (WString.h/ WString.cpp)
instread of std::string for Arduino user ?
Thank you.
nlohmann commentedon Jun 18, 2018
@TridentTD You can template the string type for the JSON library yourself.
nlohmann commentedon Jun 28, 2018
@TridentTD Do you need any further assistance?
TridentTD commentedon 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 commentedon 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:
Not sure about the prototype though.
nlohmann commentedon Jul 9, 2018
@TridentTD Any news on this?
nlohmann commentedon Jul 11, 2018
@TridentTD Does esp8266/Arduino#3358 (comment) work for you?
nlohmann commentedon Jul 21, 2018
@TridentTD Any news on this?