Search code examples
clibrariesmmap

mman.h does import mmap function but fails to import mremap


So I wanted to use the mremap function to more easily work with memory mapped files but an implicit declaration error is raised

addr = mremap(addr, len, len_file, MREMAP_MAYMOVE);

I do include the required libraries, if i didnt the line

addr = mmap(NULL, len_file, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);

would raise the same error.

My current header has the libraries that are pointed as required

#include <sys/mman.h>
#define _GNU_SOURCE

Solution

  • The order is incorrect here:

    #include <sys/mman.h>
    #define _GNU_SOURCE
    

    You must define _GNU_SOURCE before including the headers:

    #define _GNU_SOURCE
    #include <sys/mman.h>