Search code examples
c++cygwinposix

putchar_unlocked doesn't work in C++ 14 standard


I am using g++ (GCC) 4.9.3 on Cygwin. I am not able to use getchar_unlocked or putchar_unlocked with C++ 14 standard.
Consider this sample code

#include <cstdio>

int main() {
    putchar_unlocked('1');
    return 0;
}

When I compile and run with

g++ foo.cpp && a.exe && rm ./a.exe

I am getting expected output 1.
But when I do

g++ -std=c++14 foo.cpp && a.exe && rm ./a.exe

I am getting error saying putchar_unlocked was not declared.

foo.cpp: In function 'int main()':
foo.cpp:4:22: error: 'putchar_unlocked' was not declared in this scope
  putchar_unlocked('1');
                      ^

Solution

  • putchar_unlocked isn't part of any version of the C or C++ standards, and Cygwin doesn't implement any other standard that does provide putchar_unlocked.

    Cygwin does provide putchar_unlocked as a non-standard extension, but you need to actually leave non-standard extensions enabled.

    The default -std= version is -std=gnu++03 (or one of its synonyms). This is C++03 plus extensions. You changed it to -std=c++14. This is C++14 without extensions. Use -std=gnu++14 to leave extensions enabled.