views:

3080

answers:

8

I need my code to do different things, based on the operating system it gets compiled on. I'm looking for something like this:

#ifOSisWindows
  //define something for Windows
#else
  //define it for a Unix machine
#endif


Is there a way to do this? Is there a better way to do the same thing?

+13  A: 

There are predefined macros that are used by most compilers, you can find the list here

Otherwise, you will have to adjust the build system so a macro like OS_WINDOWS/OS_UNIX gets defined during compilation, then you will have to check it in the code using ifdef.

#ifdef OS_WINDOWS
   //define something for Windows
#else
  //define it for a Unix machine
#endif
hayalci
This answer is correct but the best solution is the one given by quinmars
bortzmeyer
A: 

Some compilers will generate #defines that can help you with this. Read the compiler documentation to determine what they are. MSVC defines one that's __WIN32__, GCC has some you can see with --show-defines.

davenpcj
A: 

Use #define OSsymbol and #ifdef OSsymbol where OSsymbol is a #define'able symbol identifying your target OS.

Typically you would include a central .h file defining the selected OS symbol and use OS-specific include and library directories to compile and build.

You did not specify your development environment, but I'm pretty sure your compiler provides global defines for common platforms and OSes.

See also http://en.wikibooks.org/wiki/C_Programming/Preprocessor

devio
+2  A: 

In most cases it is better to check, weather a given functionality is present or not. For example if the function pipe() exists or not. Or if a needed header file is present like windows.h or whatever.

quinmars
is there an easy way to check out if a function is defined ?
hayalci
If you are using autoconfig you can check for functions with AC_CHECK_FUNCS(). AC_CHECK_FUNCS(pipe sqrt) will define HAVE_PIPE and HAVE_SQRT if the functions are available. I don't know how it is with other building tools, but I guess they also support this in a way.
quinmars
A: 

MS compiler PreDefined Macros can be found here:

http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx

I think you are looking for:

_WIN32
_WIN64

gcc compiler PreDefined MAcros can be found here:

http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html

I think you are looking for:

__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

Do a google for your appropriate compilers pre-defined.

Martin York
+1  A: 

There is no standard macro that is set according to C standard. Some C compilers will set one on some platforms (e.g. Apple's patched GCC sets a macro to indicate that it is compiling on an Apple system and for the Darwin platform). Your platform and/or your C compiler might set something as well, but there is no general way.

Like hayalci said, it's best to have these macros set in your build process somehow. It is easily possible to set a macro with most compilers without modifying the code. E.g. GCC. You can simply tell GCC as argument

gcc -D Windows
gcc -D UNIX

And in your code

#if defined(Windows)

#elif defined(UNIX)

#else
#    error Unsupported Operating System
#endif
Mecki
A: 

You will probably find this helpful. http://predef.sourceforge.net/prestd.html

It has a listing of the various predefined defines that different compilers setup on different platforms.

Evan Teran
A: 

show GCC defines :

gcc -dM -E - < nul

gcc -dM -E - < /dev/null

Predefined macroses in MinGW : WIN32 _WIN32 WIN32 __WIN32 MINGW32 WINNT WINNT __WINNT X86 i386 __i386

on UNIXes unix, unix, __unix

qwer