next up previous contents
Next: 10.6 Additional Comments Up: 10 Porting Applications to Previous: 10.4.3 Process control under

10.5 Portable conditional compilation

If you need to make modifications to existing code in order to port it to Linux, you may need to use ifdef...endif pairs to surround parts of Linux-specific code--or, for that matter, code corresponding to other implementations. No real standard for selecting portions of code to be compiled based on the operating system exists, but many programs use a convention such as defining SVR4 for System V code, BSD for BSD code, and linux for Linux-specific code.

The GNU C library used by Linux allows you to turn on various features of the library by defining various macros at compile time. These are:

If you define _BSD_SOURCE yourself, the additional definition _FAVOR_BSD will be defined for the library. This will cause BSD behavior for certain things to be selected over POSIX or SVR4. For example, if _FAVOR_BSD is defined, setjmp and longjmp will save and restore the signal mask, and getpgrp will accept a PID argument. Note that you must still link against libbsd to get BSD-like behavior for the features mentioned earlier in this paper.

Under Linux, gcc defines a number of macros automatically which you can use in your program. These are:

Many programs use
  #ifdef linux
to surround Linux-specific code. Using these compile-time macros you can easily adapt existing code to include or exclude changes necessary to port the program to Linux, Note that because Linux supports more System V-like features in general, the best code base to start from with a program written for both System V and BSD is probably the System V version. Alternately, you can start from the BSD base and link against libbsd.


next up previous contents
Next: 10.6 Additional Comments Up: 10 Porting Applications to Previous: 10.4.3 Process control under

Converted on:
Fri Mar 29 14:43:04 EST 1996

Hosting by: Hurra Communications Ltd.
Generated: 2007-01-26 17:57:43