1

I'm using CodeBlocks and GCC compiler. I'd like to use "string safe functions" e.g strlen_s, strcpy_s, but compiler shows an error:

Undefined reference to strlen_s.

I then add a line to the code:

#define __STDC_WANT_LIB_EXT1__ 1

As well as writing the following in the Compiler Options (settings -> compiler -> global compiler settings -> other compiler options):

-std=c11

In the book that I'm reading there's a code to checking whether my compiler supports these functions. The code is as follows:

#include <stdio.h>

int main()
{
    #if defined __STDC_WANT_LIB_EXT1__
        printf("optional functions are defined");
    #else
        printf("optional functions are not defined");
    #endif 

return 0;
}

When I run this code I see "optional functions are defined". I've also reinstalled CodeBlocks but I still get these errors.

Should I install another compiler? If I should, which one will be the best?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
quark
  • 70
  • 9
  • What version of `gcc`? – TriskalJM Aug 08 '17 at 16:38
  • Version of my GCC - 4.9.2 – quark Aug 08 '17 at 18:05
  • 1
    actually the compiler is not the important piece of information, here, but the C library would be. `gcc` runs on different systems with different C library. – Jens Gustedt Aug 08 '17 at 22:00
  • 2
    GCC 4.9.2/glibc does not support the bounds-checking interface. See https://gcc.gnu.org/wiki/C11Status. Similarly Mingw with the MS std library does not support it either. In fact I know of no compiler that does, I have never used it myself. – Lundin Aug 14 '17 at 10:53

2 Answers2

1

This test is not sufficient, you should also test whether the implementation defines the macro __STDC_LIB_EXT1__.

These functions are from a part of the C standard that is called "Annex K" and that is optional. With this macro you test if your C library provides that feature, with the WANT macro defined before any includes you tell the compiler that you want to use these features from Annex K.

Annex K is much controversial, and not many public domain C libraries implement it. Many people think that its interfaces don't provide the security that it claims.

And for the book that you are reading this doesn't seem to be too reliable. But then, I may be biased on that point.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

#define __STDC_WANT_LIB_EXT1__ 1 is expected to be defined by your application - you have to define it yourself to enable the use of the bounds-checking interface functions.

In order to see if the bounds-checking interface is at all available, you need to check if __STDC_LIB_EXT1__ is defined by the compiler.

Note that no function called strlen_s exists.

Lundin
  • 195,001
  • 40
  • 254
  • 396