Search code examples
cunixgcc-warning

std18 getpagesize : implicit declaration of function + nested extern declaration


I don't understand why the function getpagesize gives me a warning for implicit declaration of function while using the c18 version of gcc.

gcc test.c -Wall -std=c18

implicit declaration of function ‘getpagesize’ [-Wimplicit-function-declaration]

nested extern declaration of ‘getpagesize’ [-Wnested-externs]

  int BLOCKSIZE = getpagesize();

And this is my included files :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdint.h>
#include <errno.h>

Solution

  • Using -std=cXX instead of -std=gnuXX disables a bunch of normally defined feature test macros, including the ones that provide getpagesize(). From its man page (Assuming you're using linux):

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

      getpagesize():
           Since glibc 2.19:
               _DEFAULT_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
           From glibc 2.12 to 2.19:
               _BSD_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
           Before glibc 2.12:
               _BSD_SOURCE || _XOPEN_SOURCE >= 500
    

    So you have to define the appropriate one to the appropriate value before including any header files. Or just use -std=gnu18.

    Edit: Also, since getpagesize() is obsolete and not standard, consider using the POSIX standard sysconf(_SC_PAGESIZE) instead.