PDA

View Full Version : identify operating system


lenna
08-21-2004, 05:30 PM
Hi,

does anybody know a library function or any other way to identify the operating system at pre compilation time (or afterwards)?

basically, I need to write a program that will run BOTH under UNIX and WINDOWS with the same code. thus I thought a good solution can be to wrap OS dependent functions under pre compilation condition, something like:

#if OS == UNIX
...use UNIX lib functions
#elseif OS == WINDOWS
...use WINDOWS lib functions

the problem I don't know how to recognize the OS at pre compilation or compilation time.

thanks,
me

RobSeace
08-21-2004, 08:47 PM
This is done all the time in multi-platform software... Pretty much
any compiler/preprocessor will pre-define some macro to identify
the OS it's running on... The exact name/value of that macro is
something you'll need to look into your compiler/preprocessor
documentation to find... But, eg: if using GCC, you can run
"cpp -dM </dev/null" to see a list of pre-defined system macros
on that given system... But, typically, you can do something
similar to this:


#if defined(__linux__)
/* do your Linux-specific code here */
#elif defined(__QNX__)
/* do QNX-specific stuff here */
#elif defined(__unix__)
/* do any non-OS-specific Unix stuff here */
#else
/* do non-Unix stuff here */
#endif


Like I say, you'll need to check your local compiler/preprocessor
documentation for details on exactly what pre-defined macro you
need to look for, but chances are whatever system you care about
WILL have some similar macro you can check for...

lenna
08-21-2004, 09:27 PM
HI RobSeace,

thanks for your great answer. I'm gonna check it out..seemed exactly what I needed.

c u :D
lenna

i3839
08-22-2004, 03:16 AM
Well, for Linux you can use "#ifdef linux" and for Windows "#ifdef _WIN32". If you write standard code you can just use "#ifdef _WIN32" to detect Windows and assume a Unix otherwise.

(Deleted the other double post and moved the above answer to this one.)

lenna
08-22-2004, 03:57 PM
that works. thanx all!