esp-idf: Missing std::to_string C++ support (libstdc++) (IDFGH-133)

I notice that std::to_string support is missing from the libstdc++, not sure what it takes to get that added, but it is useful especially when porting other C++11 code.

Specifically, the following app_main should work. The example usage is directly from cppreference: http://www.cplusplus.com/reference/string/to_string/ (it currently fails to compile)

// to_string example
#include <iostream>   // std::cout
#include <string>     // std::string, std::to_string

#ifdef __cplusplus
extern "C"
#endif // __cplusplus
void app_main()
{
  std::string pi = "pi is " + std::to_string(3.1415926);
  std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
  std::cout << pi << '\n';
  std::cout << perfect << '\n';
}

The error I see when running make:

./hello_world_main.cpp: In function 'void app_main()':
./hello_world_main.cpp:10:31: error: 'to_string' is not a member of 'std'
   std::string pi = "pi is " + std::to_string(3.1415926);
                               ^
./hello_world_main.cpp:11:25: error: 'to_string' is not a member of 'std'
   std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
                         ^
make[1]: *** [hello_world_main.o] Error 1
make: *** [component-main-build] Error 2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve been wanting to use std::to_string() too. Today I took the time to dig into the source and found that adding this to your component.mk allows usage of std::to_string(),

CPPFLAGS += -D_GLIBCXX_USE_C99

I don’t know if there are any side effects, seems to work fine though.

Half a year later - how is the time schedule looking for switching to a new gcc @igrr ?

@PerMalmberg There is a preview version of toolchain based on GCC 8.2 available now, please see https://esp32.com/viewtopic.php?f=10&t=7400.

Will keep this issue open while the new version is in preview.

@PerMalmberg I can confirm that seems to work for me. I’m applying it selectively to the files that need it (i.e. are using std::to_string).

I’m also curious about any potential downsides? And/or if we will be getting a significantly new toolchain version any time soon?

Some libstdc++ features which depend on C99 support in C library are currently unavailable, because GCC 5.2.0 incorrectly checks for these C99 features during configuration step. We plan to switch to a more recent version of GCC in the future, which will also fix this configuration issue.