5

I'm currently developing a cross-platform C application. Is there any compiler macro which is defined only during compilation on Windows, so I can #ifdef some Windows specific #includes?

Typical example is selecting between WinSock and Berkeley sockets headers:

#ifdef _WINDOWS
   #include <winsock.h>    
#else
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <sys/un.h>
   #include <arpa/inet.h>
   #include <netdb.h>
#endif

So the thing I'm looking for is something like that _WINDOWS macro.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NumberFour
  • 3,551
  • 8
  • 48
  • 72

3 Answers3

11

Your best bet is to use

_WIN32

It is guaranteed to be defined when compiling for a 32-bit or 64-bit Windows platform using the Visual C++ compiler. I would expect other compilers for Windows to define it as well (the Intel C++ compiler defines it, as does the MinGW gcc).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • And what in case of 64-bit Windows? I presume _WIN32 is defined only on 32 bit systems, right? – NumberFour Apr 05 '10 at 15:21
  • 3
    The 32 is really there to separate it from ancient 16-bit versions of Windows. Removing that definition in Win64 would've broken too much stuff, so it was less hassle to leave it in. – Matti Virkkunen Apr 05 '10 at 15:28
3

Use _WIN32.

Reference:

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
2
_WIN32  

Defined for applications for Win32 and Win64. Always defined.

_WIN64  

Defined for applications for Win64.

Source : Lists the predefined ANSI C and Microsoft C++ implementation macros.

anno
  • 5,970
  • 4
  • 28
  • 37