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.
Asked
Active
Viewed 1.6k times
22
-
Also consider using `shm_open`, which achieves a similar effect but is POSIX. – Ciro Santilli OurBigBook.com Aug 07 '15 at 12:05
-
1Unfortunately `shm_open` has many more caveats than `mmap`, and because of its unportable _name_ argument it may not be any more standard in practice. – Lassi Apr 23 '19 at 20:08
1 Answers
26
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

Conrad Meyer
- 2,851
- 21
- 24
-
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. – zwol Mar 27 '11 at 02:20
-
@Zack: Or `_BSD_SOURCE`, depending on where you want your code to be portable to, I imagine. Or am I off-base here? – Conrad Meyer Mar 27 '11 at 02:38
-
4Zack has the right answer. This is not about the C dialect but having the right feature test macros for extended library functions. – R.. GitHub STOP HELPING ICE Mar 27 '11 at 04: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. – zwol Mar 27 '11 at 04:35