GCC is non-conforming?

Viewed 425

I know that very few compilers actually support C11 threads (which is sad, but whatever). The C11 standard demands that an implementation that doesn't support threads defines __STDC_NO_THREADS__. Yet this program seems to give an error:

#include <stdio.h>
#ifndef __STDC_NO_THREADS__
    #include <threads.h> //error is here
#endif // __STDC_NO_THREADS__

int main(void)
{
    #ifdef __STDC_NO_THREADS__
        printf("There are no threads");
    #else
        printf("There are threads");
    #endif // __STDC_NO_THREADS__
}

//Error at line 3: fatal error: threads.h: No such file or directory

Compiler version is GCC 9.2.0 (Windows 10 x64), with __STDC_VERSION__ = 201710L (so it is C17). If you cannot tell, the problem is that my compiler doesn't define either __STDC_NO_THREADS__ or <threads.h>, which doesn't conform to C11. What could the problem be?

1 Answers

Pre-C11 compilers and libraries will not define __STDC_NO_THREADS__, nor will post-C11 ones with support for threading. So a correct check needs to look like this:

#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
  #include <threads.h>

Otherwise older versions of the compiler/library will not work. In your case, you seem to be using Mingw under Windows, in which case the non-compliant Microsoft CRT is used (it does not conform to C99 and beyond).

Later versions of gcc using later versions of libc seem to work correctly with the original code.

Please note that gcc is not to be regarded as a conforming implementation unless you compile with -std=c17 -pedantic-errors. I don't think it matters in this specific case.

Related