[cs631apue] NetBSD #include <bsd/stdlib.h>

Jan Schaumann jschauma at stevens.edu
Wed Dec 2 21:07:54 EST 2015


Patrick Murray <pmurray1 at stevens.edu> wrote:
> When testing on the linux lab, BSD's standard library is included using
> <bsd/stdlib.h>; however, on NetBSD this is simply <stdlib.h>. How can we
> make our headers/makefile (-lbsd) portable on both the linux lab and your
> AWS instance?

You'd want to do conditional includes based on definitions given on a
platform.  Every OS usually defines a custom preprocessor definition;
you can view all preprocessor definitions via the 'cpp -dM </dev/null'
command, for example.

So to find out what value might be defined for a given OS, you could run

cpp -dM </dev/null | grep -i $(uname)

which, on NetBSD, should yield

#define __NetBSD__ 1

while on Linux, it might yield

#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1


With that information, you can then use

#include <stdlib.h>
#ifdef __linux
#include <bsd/stdlib.h>
#endif

(This also explains why you can't have a variable named 'linux' on Linux
systems:
http://stackoverflow.com/questions/19210935/why-does-the-c-preprocessor-interpret-the-word-linux-as-the-constant-1)

-Jan


More information about the cs631apue mailing list