1

I'm using an ESP8266 with the Arduino IDE to work on a microcontroller project. However I also want to use it as an opportunity to learn more about C++. I'm trying to deserialize a string like "key1:value1;key2:value2;..." into a map of strings, and I came across this question which seemed relevant: Parsing a comma-delimited std::string

So I put it into a function like

void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
  std::stringstream ss(input);

  std::string key;
  std::string value;

  while(ss.good()) {
    std::getline(ss, key, ':');
    std::getline(ss, value, ';');
    output->insert(std::make_pair(key, value));
  }
}

However I'm getting compiler errors (copied from IDE) and unfortunately I can't figure out what I need to fix. I did isolate the problem to the line std::stringstream ss(input); but I'm not sure what the issue is.

Arduino: 1.6.13 (Mac OS X), Board: "Adafruit HUZZAH ESP8266, 80 MHz, 4M (3M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 921600"

/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o):(.literal._ZSt9use_facetISt5ctypeIcEERKT_RKSt6locale[std::ctype<char> const& std::use_facet<std::ctype<char> >(std::locale const&)]+0x4): undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::ctype<char> const& std::use_facet<std::ctype<char> >(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()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::numpunct<char> const& std::use_facet<std::numpunct<char> >(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()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > >(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()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > >(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()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/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 to `std::__throw_bad_cast()' follow
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o):(.literal._ZNSt6locale5facet18_S_create_c_localeERPiPKcS1_+0x4): undefined reference to `std::__throw_runtime_error(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o): In function `std::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*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o):(.literal._ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate[std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)]+0x4): undefined reference to `std::__throw_ios_failure(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o): In function `std::basic_ios<char, std::char_traits<char> >::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*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(istream-inst.o): In function `__check_facet<std::ctype<char> >':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/istream:95: undefined reference to `std::__throw_bad_cast()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Adafruit HUZZAH ESP8266.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks in advance!

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
Jake Dluhy
  • 617
  • 6
  • 19
  • Read the answer in this question: https://stackoverflow.com/questions/41378849/linking-error-using-arduino-library-with-cs-stringstream – Zebrafish Feb 23 '18 at 07:47

1 Answers1

0

Thanks to Zebrafish for pointing me to this question about sstream with Arduino. Apparently it is difficult to include a lot of std c++ in Arduino projects, including stringstream.

This led me to find this answer, which recommends staying away from Arduino String. I used it to revise my function to this:

#include <string>
#include <cstring>
#include <map>

void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
  char* inputChars = const_cast<char*>(input.c_str());

  char* key;
  char* value;

  key = strtok(inputChars, ":");
  value = strtok(NULL, ";");

  while(key != NULL && value != NULL) {
    output->insert(std::make_pair(key, value));

    key = strtok(NULL, ":");
    value = strtok(NULL, ";");
  }
}
Jake Dluhy
  • 617
  • 6
  • 19