First, don't forget getenv
to read an environment variable into a string, if you can get away with using a plain string instead of a preprocessor variable. E.g.,
#include <stdlib.h>
char *ug = getenv("USER_GRADHA");
if (ug) {
//use the environment variable here
} else {
//it's NULL == undefined env variable (in all likelihood)
}
If you need to have a preprocessor variable, the shell can set one at compiler invocation using the POSIX-standard -D or -U flag. Unfortunately, the simple
gcc -DUSER_GRADHA=$USER_GRADHA file.c
is likely to cause you trouble, because if the environment variable is undefined, you'll have a blank but defined preprocessor variable. So it may be easiest to use a shell conditional, like
if [ $USER_GRADHA ] ; then ug="-D$USER_GADHA"; else ug="-U$USER_GRADHA"; fi
c99 $ug file.c
This is easy to implement in a makefile or many other sorts of hooks to execute arbitrary shell code for compilation.
Finally, there's always Autotools, which is designed for exactly this problem of compiling based on information about the environment. You asked about Xcode, so Autotools may not fit your workflow (and a lot of people don't understand Autotools and dismiss it out of hand), but it's there if you want it.