Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implicit declaration of 'memfd_create'

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

main.c:9:15: warning: implicit declaration of function 'memfd_create' is invalid in C99 [-Wimplicit-function-declaration]
        int32_t fd = memfd_create("", 0);

>Solution :

_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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading