NumberFour
NumberFour

Reputation: 3631

Determining whether compiling on Windows or other system

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.

Upvotes: 5

Views: 1949

Answers (3)

James McNellis
James McNellis

Reputation: 355327

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).

Upvotes: 11

Josh Kelley
Josh Kelley

Reputation: 58442

Use _WIN32.

Reference:

Upvotes: 3

anno
anno

Reputation: 5989

_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.

Upvotes: 2

Related Questions