Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__STDC_LIB_EXT1__ availability in gcc and clang

Since a quick Google search did not find anything, I will try to ask here (since many people involved in gcc/clang hang around here) - What is the status of __STDC_LIB_EXT1__ in gcc/clang? We are developing a cross platform applicataion and I wanted to use some of the safe bounds checking functions from <stdio.h> (which by miracle are available on Visual Studio 2017), but could not compile the code with Xcode 9.2. I assumed maybe the clang version Xcode uses is outdated, but gcc 6.3.0 on Ubuntu behaves the same. I am trying to use tmpnam_s with the following sample:

#if defined(__STDC_LIB_EXT1__)
#define  __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#else
#error "__STDC_LIB_EXT1__ not defined"
#endif

int main(int argc, char** argv)
{
    char t[L_tmpnam_s]; 
    tmpnam_s(t, L_tmpnam_s);
    return 0;
}

But the compilation fails with the macro not being defined:

gcc -std=c11 test.c
test.c:5:2: error: #error "__STDC_LIB_EXT1__ not defined"
#error "__STDC_LIB_EXT1__ not defined"
^~~~~

Am I doing something wrong or this function set is simply poorly supported?

like image 599
Rudolfs Bundulis Avatar asked Dec 18 '17 11:12

Rudolfs Bundulis


People also ask

What is __ stdc_lib_ext1 __?

The macro __STDC_LIB_EXT1__ is defined as 200509L to indicate the presence of C Library Extension 1. If you define the macro __STDC_WANT_LIB_EXT1__ as zero before you include any of the headers that contain C Library Extension 1 additions, then none of these additions will be visible to the program.

Does Clang support GCC extensions?

In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions.

Why does Clang use GCC?

Clang is designed to provide a frontend compiler that can replace GCC. Apple Inc. (including NeXT later) has been using GCC as the official compiler. GCC has always performed well as a standard compiler in the open source community.


1 Answers

The whole set of 'safe' functions with the _s suffixes is poorly supported. Microsoft wrote a set of functions with the _s suffixes and submitted that to the C standard committee to be standardized. The committee made some changes (arguably out of necessity), and created a technical report, TR 24731-1. A mildly modified version of the TR was included as optional Annex K (normative) in the C11 standard, ISO/IEC 9899:2011.

You can find many sordid details in the answers to Do you use the TR-24731 "safer" functions?, especially in the notes in my answer to that question, and especially the link to the Standard C committee document N1967 Field Experience with Annex K — Bounds Checking Interfaces.

I don't know what the current status of the N1967 proposal is, but that it was suggested is telling. N1967 also contains links to libraries that support Annex K / TR-24731-1 — the list is limited.

Note that Microsoft does not implement the library specified by the C11 standard. It implements an approximation to the standard, but there are crucial differences. This would matter more if any other system had implemented the standard — but the functions have not been implemented in any widely accepted form (so, for example, the GNU C Library does not and will not support them).

like image 50
Jonathan Leffler Avatar answered Oct 06 '22 03:10

Jonathan Leffler