slamdunker
slamdunker

Reputation: 67

Using non-standard functions in Code::Blocks

I got this book "Beginning C" by Ivor Horton and I'm half way through it and I like it; so far so good. I use Code::Blocks on Windows as my IDE, and now I've run into the problem I cannot solve for about 3 days now.

The author mentions some "optional" functions in <string.h>, like strnlen_s(), and also says that these are available in the new standard — C11 (the book is from 2013; I don't know how new C11 actually is), and he also gives a piece of code that will determine "whether the standard library that comes with your C compiler supports these optional functions". This is the code:

#include <stdio.h>

int main(void)
{
#if defined __STDC_LIB_EXT1__
    printf("Optional functions are defined.\n");
#else
    printf("Optional functions are not defined.\n");
#endif
    return 0;
}

So I run the code to check if GCC in Code::Blocks does and determine that it doesn't. The book didn't recommend the compiler nor the IDE; I picked up Code::Blocks with GCC on my own, since that's what I do my exams in at college, so I figured I should get familiar with the environment.

The thing is, I have no idea how to "fix" this, since strnlen() doesn't work, strnlen_s() doesn't work, and bunch of others, and I can't really continue through a book. Not that I need them, or that I can't do it any other way (strlen() works just fine) but it would be nice to know how to use non-standard functions.

Upvotes: 2

Views: 490

Answers (3)

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37914

The optional Annex K from the C11 Standard is not widely adopted yet (see Deduplicator's comment below). For instance as of February 2015 it hasn't been merged into glibc.

The good news is that you might try an alternative compiler. For instance Pelles C for Windows is a modified LCC with enhanced support for newest C11 features (like atomics and C11 threads model, that I believe are also mentioned in your book). Here is some basic program, that compiles and runs in it:

#include <stdio.h>
#include <string.h>

int main(void)
{
#if defined __STDC_LIB_EXT1__
    printf("Optional functions are defined.\n");
#else
    printf("Optional functions are not defined.\n");
#endif

    char *str = "Hello Annex K";
    printf("%zu\n", strnlen_s(str, 5));

    return 0;
}

Output is:

Optional functions are defined.
5
Press any key to continue...

Upvotes: 2

Deduplicator
Deduplicator

Reputation: 45654

You are speaking of the optional Annex K Microsoft pushed through.

K.2 Scope

1 This annex specifies a series of optional extensions that can be useful in the mitigation of security vulnerabilities in programs, and comprise new functions, macros, and types declared or defined in existing standard headers.
2 An implementation that defines __STDC_LIB_EXT1__ shall conform to the specifications in this annex.380)
3 Subclause K.3 should be read as if it were merged into the parallel structure of named subclauses of clause 7.

It is generally seen as deeply flawed, and Microsoft trying to force it's use as a severe nuisance.
That's especially the case as they are the only major player implementing them, and their versions are non-conformant.

glibc with gcc for example provide most supposed advantages of that annex without introducing new functions, discouraging use of half the standard-library and forcing such a cumbersome API on programmers.

You might want to read the C tag-wiki, and especially grab a draft of the C11 standard (which is from 2011, as the name should imply).

Upvotes: 3

Riot
Riot

Reputation: 16696

Up to date versions of GCC certainly do support C11, you need to enable it with the compiler flag -std=c11.

I presume you're using some flavour of MinGW with Code::Blocks - I recommend using MinGW-W64 as it is actively maintained and very up to date.

Also, bundled toolchains of MinGW-W64's gcc are available at TDM-GCC.

The Code::Blocks IDE itself doesn't care which version of C you're using, that doesn't affect what libraries you have available.

Upvotes: 3

Related Questions