[solved] Can't use stringstream

Hi,

I’m using platformio with visual studio code and have a problem with sstream include. When i add include <sstream> and code like this

std::ostringstream ss;
ss << "/temp/";
ss << i;

I’m getting compilation errors:

Compiling .pioenvs/d1_mini_pro/src/wifi_server.cpp.o
Linking .pioenvs/d1_mini_pro/firmware.elf
/home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(ostream-inst.o):(.literal.ZNSo9_M_insertIlEERSoT[std::basic_ostream<char, std::char_traits >& std::basic_ostream<char, std::char_traits >::_M_insert(long)]+0x0): undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(ostream-inst.o): In functionstd::basic_ostream<char, std::char_traits >& std::basic_ostream<char, std::char_traits >::_M_insert(long)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/ostream:544: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(wlocale-inst.o): In functionstd::ctype<wchar_t> const& std::use_facet<std::ctype<wchar_t> >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_facets_nonio.tcc:1173: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(wlocale-inst.o): In functionstd::numpunct<wchar_t> const& std::use_facet<std::numpunct<wchar_t> >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_facets_nonio.tcc:1173: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(wlocale-inst.o): In functionstd::moneypunct<wchar_t, true> const& std::use_facet<std::moneypunct<wchar_t, true> >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_facets_nonio.tcc:1173: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(wlocale-inst.o):/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_facets_nonio.tcc:1173: more undefined references tostd::__throw_bad_cast()’ follow
/home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(ios-inst.o):(.literal._ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate[std::basic_ios<char, std::char_traits >::clear(std::_Ios_Iostate)]+0x4): undefined reference to std::__throw_ios_failure(char const*)' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(ios-inst.o): In functionstd::basic_ios<char, std::char_traits >::clear(std::_Ios_Iostate)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/basic_ios.tcc:122: undefined reference to std::__throw_ios_failure(char const*)' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(ios-inst.o): In function__check_facet<std::ctype >’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/basic_ios.tcc:122: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(locale-inst.o): In functionstd::ctype const& std::use_facet<std::ctype >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_classes.tcc:114: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(locale-inst.o): In functionstd::numpunct const& std::use_facet<std::numpunct >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_classes.tcc:114: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(locale-inst.o): In functionstd::num_put<char, std::ostreambuf_iterator<char, std::char_traits > > const& std::use_facet<std::num_put<char, std::ostreambuf_iterator<char, std::char_traits > > >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_classes.tcc:114: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(locale-inst.o): In functionstd::num_get<char, std::istreambuf_iterator<char, std::char_traits > > const& std::use_facet<std::num_get<char, std::istreambuf_iterator<char, std::char_traits > > >(std::locale const&)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/include/bits/locale_classes.tcc:114: undefined reference to std::__throw_bad_cast()' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(locale-inst.o):/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: more undefined references tostd::__throw_bad_cast()’ follow
/home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(c++locale.o):(.literal.ZNSt6locale5facet18_S_create_c_localeERPiPKcS1+0x4): undefined reference to std::__throw_runtime_error(char const*)' /home/skoczo/.platformio/packages/framework-arduinoespressif8266/tools/sdk/lib/libstdc++.a(c++locale.o): In functionstd::locale::facet::_S_create_c_locale(int*&, char const*, int*)’:
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc+±v3/src/c++98/c++locale.cc:207: undefined reference to `std::__throw_runtime_error(char const*)’
collect2: error: ld returned 1 exit status
*** [.pioenvs/d1_mini_pro/firmware.elf] Error 1

Do you have idea what can be wrong?

Does it work within Arduino IDE?

Hm I found

which suggest that it’s either a problem with the stripped-down C++ library Arduino is using or a problem with the C++ implementation of the compiler’s C++ library.

I didn’t try in arduino. I don’t even know how to run it in arduino.

Thanks. This is really helpful. This is interesting part ‘you cannot easily include sstream in your project nor most other C++ libraries’.

So the response is to convert int to string using ‘String(i, DEC)’ instead of stringstream

Yes I’d also use only standard Arduino types.

There’s something interesting though: These missing functions were once implemented in an abi.cpp in some file revision (Arduino/abi.cpp at 775b0a0c988efa6b646b077e17287623f28bee13 · esp8266/Arduino · GitHub) but were later removed again in Arduino/abi.cpp at master · esp8266/Arduino · GitHub via commit Enable exceptions, update to optimized newlib, migrate to new toolcha… · esp8266/Arduino@6280e98 · GitHub.

The following code makes it work by force:

#include <Arduino.h>
#include <sstream>

void my_panic() {
	Serial.println("PANIC!!!");
	//hangup
	while(true) ;
}

namespace std {
void __throw_bad_cast(void)
{
	my_panic();
}

void __throw_ios_failure(const char* str)
{
    (void) str;
    my_panic();
}

void __throw_runtime_error(const char* str)
{
    (void) str;
    my_panic();
}
}

void setup() {
	Serial.begin(115200);

	int i = 10;
	std::ostringstream ss;
	ss << "/temp/";
	ss << i;

	std::string s = ss.str();
	//print out the std::string
	Serial.println(s.c_str());
}

void loop() {

}