2

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
slamdunker
  • 67
  • 6

3 Answers3

3

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
Riot
  • 15,723
  • 4
  • 60
  • 67
  • So I included -std=c11 in Project-Build options...-Debug- Other options tab. (Did I do it right?) And nothing changes, the code from the OP still outputs that optional functions are not defined. I got the first version for windows from here http://www.codeblocks.org/downloads/26 , the "recommended" one if you don't know what you're doing, and I certainly don't. Did I mess up? Shouldn't that one have MinGW you're talking about? – slamdunker Feb 17 '15 at 21:54
  • Yes - you can either put it in project-wide "other options", or the "other options" specific to one or more targets, or alternatively you can set it globally by going to settings->compiler->global compiler settings->other options there. – Riot Feb 17 '15 at 21:56
  • Let me try the last thing, because project-wide doesn't work. Also, now I see that I might've screwed up, I'm not sure if I download the first one like I said, or the actual recommended one, which would be the second on that link. It was 2 months ago. – slamdunker Feb 17 '15 at 21:59
  • Nope, still the same thing. :S – slamdunker Feb 17 '15 at 22:01
  • @slamdunker: Just ignore that MS is pushing annex K and ignore it completely. – Deduplicator Feb 17 '15 at 22:08
  • Ugh, screw it, I cannot do it, I can't even set up the compiler with the link you gave me. I give up, I'll just use other functions. Thanks everyone tho! – slamdunker Feb 17 '15 at 22:18
3

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).

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 2
    See also [Do you use the TR 24731 'safe' functions](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions), which has answers noting where Microsoft's variant of the function deviates from the standard variant of the functions with the same names, which limits their usability. TR 24731 was a technical report that was a precursor to what became Annex K in C11. Also, few (if any) vendors other than Microsoft have actually implemented the functions. – Jonathan Leffler Feb 17 '15 at 21:46
  • So basically the book author is a sell out? :D That's what many people told me, "scrap the book, use K&R", but the thing is, it's not about the "secure" functions anymore, or if they're good or not. I just hate that I'm stuck on the thing that I CAN'T do, not the thing that I will or want to do. – slamdunker Feb 17 '15 at 21:56
  • @slamdunker: The thing is that those interfaces in annex k are dysfunctional, and if MS hadn't pushed so hard they wouldn't have made it even to optional status. If they stopped their FUD campaign, their *special* implementation would die a silent death even on windows. – Deduplicator Feb 17 '15 at 22:23
  • @slamdunker: The book author made use of functions that are an optional part of the C11 standard, and a part which many implementations have decided not to implement. He was able to use Microsoft's compilers and runtime library support for (approximations to) these functions. He may not have realized, or may not have cared, that other implementers have chosen not to implement the functions. Many of the functions can be written without difficulty unless you want to simulate the error reporting mechanisms. Some are harder to implement: the exclusive file access code for `fopen_s()`, for example. – Jonathan Leffler Feb 17 '15 at 22:26
  • @JonathanLeffler, is there a way to find Microsoft's compilers and use them in Code::Blocks or would I have to download a 6gb MS' IDE just so I can use a handful of functions I might never need in console applications I make with my level of knowledge, eg. tic tac toe. – slamdunker Feb 17 '15 at 22:30
  • 1
    @slamdunker: No need to get the IDE, "just" the platform-sdk would suffice. Still, 2.5 GB isn't that little. http://www.microsoft.com/en-us/download/details.aspx?id=3138 – Deduplicator Feb 17 '15 at 22:36
  • So when I download this, I'll be able to use some files from it, "through" code::blocks, to compile my programs in code::blocks? I'm sorry I'm bothering you for so long, but I really just began messing about with this, the only IDE I ever used is Code::blocks, and I didn't even know what compiler is it using by default until I ran into this problem and I googled around. – slamdunker Feb 17 '15 at 22:43
  • Yes, you can configure code::blocks to use those. Still, I don't use it so I don't know how. People having done so are probably legion even without me... – Deduplicator Feb 17 '15 at 22:45
2

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...
Community
  • 1
  • 1
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • Not yet? More like, it was violently rejected for cause, and as it does not get any better with age, there's no reason to suspect any change to that. – Deduplicator Feb 17 '15 at 22:24
  • I could've solved the issue by downloading the Visual Studio (I think, that's what I've been said), but I'm not interested in switching the IDE right now, and as far as I see, Pelles C is a whole new IDE, not just a compiler. Thanks for the input tho! – slamdunker Feb 17 '15 at 22:25
  • @Deduplicator: I believe that there are some valid reasons for that. Maybe the same are for ``, as POSIX threads are much more common and it will stay in that way for a long time. – Grzegorz Szpetkowski Feb 17 '15 at 22:31
  • 2
    @GrzegorzSzpetkowski, no threads are a completely different thing here, they are (slowly) getting implemented. The reaction to annex K by the open source community can simply be described as allergic, and I don't think that this is going to happen. – Jens Gustedt Feb 17 '15 at 22:39