How to use to_string and stoi under Cygwin (C++)
Ever since the new C++ standard appeared I have been trying to keep up with the state of C++, even if I don’t have a clear project in mind. And every time I try to write a bit more code I bump into the issue of missing to_string under Cygwin – I prefer using cygwin since I have Windows on my desktop and on my laptop (don’t get me started on the NVIDIA Optimus support in Linux).
So here’s the thing. However one would compile, whatever you’d include, std::to_string is undefined, even with the latest version of the GCC compiler. The reason is quite silly – the support for C11 is incomplete under Cygwin (missing support for long double type), therefore there is no proper support for vsprintf therefore a decision has been made to define it as broken. If you do that, to_string is undefined (actually it’s a bit more complicated than that, but the system header files are a mess). What can you do?
Define _GLIBCXX_USE_C99 to 1. This will tell the standard library that you’ll only use C99 level types (this doesn’t exclude C++11/14 types) and then the std::to_string and std::stoi functions will be enabled.
If you’re using cmake, you could add to your project:
add_definitions(-D_GLIBCXX_USE_C99=1) # proper to_string definition