I'm trying to use functions from mman.h
in my c code. The code compiles fine with g /clang , but when using gcc/clang it says that memfd_create
has not been declared, however the code still runs fine.
I tried compiling online with godbolt and it's the same as locally, so I doubt it's something wrong with my setup. Does anyone know why this is happening? I'm using gcc 11.3 and clang 14.
#include <stdint.h>
#include <stdio.h>
#define _GNU_SOURCE
#include <sys/mman.h>
int main()
{
int32_t fd = memfd_create("", 0);
if (fd == -1)
{
printf("Error creating fd\n");
return -1;
}
return 0;
}
Compile warning:
main.c:9:15: warning: implicit declaration of function 'memfd_create' is invalid in C99 [-Wimplicit-function-declaration]
int32_t fd = memfd_create("", 0);
CodePudding user response:
_GNU_SOURCE
has to be before any #include
. See man feature_test_macros
.
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>