FreeBSD: Defined implicit getpagesize declaration with _POSIX_C_SOURCE = 200809L.

I am currently porting some OS related feature of a software project from Linux to FreeBSD. So I recognized the following problem using getpagesize

if 10.1 is defined on _POSIX_C_SOURCE=200809L


I have created a small test program

#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
        int i = getpagesize(); 

        return 0;


If I compile use

cc test.c -o test 


it compiles without any warnings. But if I define _POSIX_C_SOURCE=200809L

(the result of a correct POSIX function definition getline

that I need in other parts of the code), I get:

cc test.c -D_POSIX_C_SOURCE=200809L
test.c:5:10: warning: implicit declaration of function 'getpagesize' is invalid in C99 [-Wimplicit-function-declaration]
        int i = getpagesize(); 


Although I have included unistd.h

as stated in the man page getpagesize

. How can I make my code compile without warnings with still defined _POSIX_C_SOURCE



source to share

1 answer


is an incorrect definition. You need _XOPEN_SOURCE

. For example:

cc -D_XOPEN_SOURCE=700 test.c 



cc -D_XOPEN_SOURCE=600 test.c 



and 700

stands for the version of the Single Unix Specification (SUS for short, aka Open Group Specification, aka POSIX) that your application expects from the system library. See here for SUSv7.

(2) NO. This may still not work because it getpagesize()

is a BSD-specific feature that can actually be hidden if you try to compile the file to POSIX conformance mode.

You usually don't need anything special to access BSD functionality on a BSD system, but the portable way is to provide a _BSD_SOURCE


A more portable POSIX compliant way to get the page size sysconf(_SC_PAGE_SIZE)

. the FreeBSD man page .

PS You don't have BSD to check it out.



All Articles