[svlug] GCC predefined preprocessor values

Seth David Schoen schoen at loyalty.org
Fri Jan 18 09:20:02 PST 2002


Rick Kwan writes:

> Hey folks,
> 
> I'm trying to compile some code with GCC on Linux, Solaris, and Mac
> OS X.  Amazingly, I have access to all three.  I'm discovering quickly
> that I want to use some OS-dependent preprocessor values.  The problem
> is, I can't fine them.  I would like to be able to say
>     #ifndef Darwin
>         freehostent (hp);
>     #endif
> except that "Darwin" is not recognized.  ("__MACH__" is.)  I haven't
> been able to find this in the GCC documentation I've read so far
> either.  It would be fun to say
>         printf ("OS is %s\n", __OS__);
> and have the right value come out, but I can't find anything of the
> sort.  In fact, it would be really nice if these were reflective of
> "uname -a".  So far, I can't fine any correlation.
> 
> Any ideas?

You can move this out into your Makefile.  Using GNU make:

OS = $(shell uname)

foo: foo.c
	cc -D__OS__=\"$(OS)\" -o foo foo.c

(This worked for me using the sample printf you provided.)

Moving this into a Makefile is somewhat usual; the approach of something
like autoconf takes this one step further (in the sense that you get a
script which runs many commands locally and uses their outputs to change
things, including defines, in order to make the compile come out right).

-- 
Seth David Schoen <schoen at loyalty.org> | Reading is a right, not a feature!
     http://www.loyalty.org/~schoen/   |                 -- Kathryn Myronuk
     http://vitanuova.loyalty.org/     |




More information about the svlug mailing list