Using std::to_string (C++11)

Asked by Mike F.

Hello,

I'm using C++11 in my current STM32F4 based project, and I'm making use of the GNU Tools for ARM Embedded Processors on Windows 7 64-bit (Thank you so much for this toolchain).

I'm trying to use the std::to_string function (new in C++11), but I get the following compilation error:
 error: 'to_string' was not declared in this scope

I'm compiling with these flags:
arm-none-eabi-g++ -ffreestanding -mthumb -mcpu=cortex-m4 -mtune=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mlittle-endian -ffunction-sections -fsingle-precision-constant -fmessage-length=0 -Wa,-EL -std=c++11

If, however, I compile with the following, it works fine:
arm-none-eabi-g++ -D _GLIBCXX_USE_C99=1 -D _GLIBCXX_USE_C99_CHECK=1 -D _GLIBCXX_USE_C99_DYNAMIC=1 -D _GLIBCXX_USE_C99_LONG_LONG_DYNAMIC=1 -ffreestanding -mthumb -mcpu=cortex-m4 -mtune=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mlittle-endian -ffunction-sections -fsingle-precision-constant -fmessage-length=0 -Wa,-EL -std=c++11

I figured this out by navigating header files, but it feels like a hack. Is there some reason why to_string cannot be used without these extra defines?

Thanks,
Mike

Question information

Language:
English Edit question
Status:
Answered
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Terry Guo (terry.guo) said :
#1

I just scanned the log file of tool chain building and found this feature is disabled for arm-none-eabi targets. Some tests during the configuration of libstdc++ failed, so the required macro _GLIBCXX_USE_C99 is disabled. We might miss something when configure libstdc++, but I need some time to investigate. Thanks for your reporting.

Revision history for this message
Terry Guo (terry.guo) said :
#2

I think I found the reason. A email is posted to libstdc++-v3 mailing list to seek a proper way to fix this issue. Please watch http://gcc.gnu.org/ml/libstdc++/2013-10/msg00245.html .

Revision history for this message
Mike F. (slavo5150) said :
#3

Terry, Thanks for looking into this. I vaguely understand the problem now, but do you have a recommended solution?

Revision history for this message
Terry Guo (terry.guo) said :
#4

A complete solution needs the fully C99 support from Newlib which I think gonna take some time. For the time being, how about using sprintf or snprintf function like below:

#include <iostream>
#include <string>
#include <cstdio>

int main ()
{
  double f = 23.43;
  char f_str[200];
  sprintf(f_str, "%Lf", f);
  std::cout << f_str << '\n';
  return 0;
}

This case works for me.

Can you help with this problem?

Provide an answer of your own, or ask Mike F. for more information if necessary.

To post a message you must log in.