I have an application that uses the mmap system call, I was having an issue getting it to compile for hours looking as to why I was getting MAP_ANON and MAP_ANONYMOUS were undeclared, I had a smaller section of code that I used and I saw I could compile it just fine so I tried just a basic compile and that worked, I saw that it fails when you add -std=c99. Is there a specific reason that MAP_ANON and MAP_ANONYMOUS are not valid in the C99 standard? I know that they aren't defined by POSIX but are defined by BSD SOURCE so I just want to know why that is.
1 Answer
You probably want -std=gnu99
instead of -std=c99
. C99 mode explicitly disables (most) GNU extensions.
I wrote a simple test:
#include <sys/mman.h>
int a = MAP_ANONYMOUS;
In C99 mode, it doesn't find the value:
$ gcc -std=c99 -c d.c
d.c:3:9: error: ‘MAP_ANONYMOUS’ undeclared here (not in a function)
Whereas in Gnu99 mode, it does:
$ gcc -std=gnu99 -c d.c
-
20If you don't want any of the GNU extensions to the C language, you can instead put
#define _GNU_SOURCE
above all#include
s in the files that need something that's not in C99 proper.– zwolCommented Mar 27, 2011 at 2:20 -
@Zack: Or
_BSD_SOURCE
, depending on where you want your code to be portable to, I imagine. Or am I off-base here? Commented Mar 27, 2011 at 2:38 -
4Zack has the right answer. This is not about the C dialect but having the right feature test macros for extended library functions. Commented Mar 27, 2011 at 4:12
-
6
_GNU_SOURCE
is a superset of_BSD_SOURCE
on platforms that implement it (i.e. glibc). You would need to define something else for an actual BSD-derived Unix, and something else again for each of the remaining proprietary variants you actually cared about. Unfortunately you can't just define all of them and be done, because some systems think they know what other systems'*_SOURCE
macros mean, and usually they're wrong; you have to figure out which system you've got, without including any system headers, and define only the appropriate one.– zwolCommented Mar 27, 2011 at 4:35
shm_open
, which achieves a similar effect but is POSIX.shm_open
has many more caveats thanmmap
, and because of its unportable name argument it may not be any more standard in practice.