Identifying the target platform

From GNUstepWiki

(Difference between revisions)
Revision as of 20:23, 10 November 2005
Comrade (Talk | contribs)
Add "standard" macros and a tip for Makefiles
Next diff →

Revision as of 20:23, 10 November 2005

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)

Using GNUStep-Make's support

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


and then the #if wrappers will look like:

#if defined (freebsd)
#elif defined (netbsdelf)