Identifying the target platform

From GNUstepWiki
Revision as of 20:23, 10 November 2005 by Comrade (talk | contribs) (Add "standard" macros and a tip for Makefiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

A common problem is identifying the target platform at compile-time so that specific code can be used, perhaps to work around a quirk, or to access a non-standard feature of the target platform.

Standard compiler platform macros

The following table is a partial extract of [Pre-defined C/C++ Compiler Macros], part of a useful resource to compiler macro definitions.

Please update the table carefully with your platform or corrections:

Platform macros
Platform Defined macro Other notes
FreeBSD __FreeBSD__ Consider using the BSD macro
Linux linux or __linux
MacOS X __MACOSX__ or __APPLE__
NetBSD __NetBSD__ Consider using the BSD macro
OpenBSD __OpenBSD__ Consider using the BSD macro
Solaris sun or __sun SunOS versions < 5 will not have __SVR4 or __svr4__ defined
Windows _WIN32 or __WIN32__

You can use #if wrappers like:

#if defined (__FreeBSD__)
...
#elif defined (__linux) || defined (linux)
...
#else
...
#endif

Using GNUStep-Make's support

Chris Vetter shared a tip on the use of your GNUmakefile.preamble with the following line:

ADDITIONAL_OBJCFLAGS += -D$(GNUSTEP_HOST_OS)

and then the #if wrappers will look like:

#if defined (freebsd)
...
#elif defined (netbsdelf)
...
#else
...
#endif