Use of #ifdef for portability - a specific case
In Solaris, the protoptype of psignal() is in siginfo.h. However, under Linux and Mac OS X, the protoptype of psignal() is in signal.h; siginfo.h does not exist. Some advocate writing 2 different .c files. Let's say I do not want to do that for such a minor discrepancy. How do I write a C program that can run on all 3 operating systems, by using #ifdef for example? That is, how can I do something like:
Code:
#ifdef SOLARIS
# include <siginfo.h>
#else
# include <signal.h>
#endif
int main ()
{ ...
... psignal() ...
...
}
Thanks.
Many thanks, Ancient Dragon and Dave_Sinkula
I found that with GCC, the command "touch foo.h; cpp -dM foo.h", assumed foo.h does not exist, prints out all GCC's predefined macros. And in Solaris 9, a macro named SVR4 is predefined with a value of 1. In GNU/Linux 2.4 and Mac OS X (the Darwin kernel), no such macro is defined, nor something like BSD for example. (On the other hand, sparc, __sparc__, unix, __unix, __unix__, sun, __sun, etc are predefined in Soalris; __unix, __linux, etc are predefined in Linux; __APPLE__, __MACH__, __POWERPC__, etc are predefined in Mac OS.)
So I guess in my particular case, I simply have to write
#ifdef SVR4
# include <siginfo.h>
#else
# include <signal.h>
#endif
unless and until I encounter an OS that predefines SVR4 but does not have <siginfo.h>, or an OS that does not predefine SVR4 but has psignal() declared in <signal.h>. Then I have to find a way to somehow further distinguish among them, assumed I do not use a tool such as autoconf.