Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 892
NumberFour Avatar asked Apr 05 '10 15:04

NumberFour


People also ask

How can I tell which Windows compiler is installed?

Type “gcc –version” in command prompt to check whether C compiler is installed in your machine.

How do I know if I have C++ compiler on Windows?

So if you ever need to check the version of the GCC C++ compiler that you have installed on your PC, you can do it through the command prompt by typing in the single line, g++ --version, and this will return the result.

How do you check is C++ installed or not?

To check if you have it installed, you can type cc or gcc at the command prompt. If for some reason it is not installed on your system, you can download it from gcc.gnu.org/install.

How do I find my CPP operating system?

To check the operating system of the host in a C or C++ code, we need to check the macros defined by the compiler (GNU GCC or G++). For example, on Windows platform, the compiler has a special macro named _WIN32 defined. So, if the macro _WIN32 is defined, we are on Windows.


3 Answers

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

like image 104
James McNellis Avatar answered Sep 21 '22 06:09

James McNellis


Use _WIN32.

Reference:

  • Microsoft C++ Predefined Macros
  • GCC Predefined Macros
  • How to get predefined macros for OS X (or just check for __APPLE__)
like image 40
Josh Kelley Avatar answered Sep 22 '22 06:09

Josh Kelley


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

like image 37
anno Avatar answered Sep 23 '22 06:09

anno