Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?

I often see __WIN32, WIN32 or __WIN32__. I assume that this depends on the used preprocessor (either one from visual studio, or gcc etc).

Do I now have to check first for os and then for the used compiler? We are using here G++ 4.4.x, Visual Studio 2008 and Xcode (which I assume is a gcc again) and ATM we are using just __WIN32__, __APPLE__ and __LINUX__.

like image 406
math Avatar asked Jun 07 '10 13:06

math


People also ask

How do you define win32?

The symbol _WIN32 is defined by the compiler to indicate that this is a (32bit) Windows compilation. Unfortunately, for historical reasons, it is also defined for 64-bit compilation. The symbol _WIN64 is defined by the compiler to indicate that this is a 64-bit Windows compilation.

What is_ msc_ VER?

_MSC_VER Defined as an integer literal that encodes the major and minor number elements of the compiler's version number. The major number is the first element of the period-delimited version number and the minor number is the second element. For example, if the version number of the Microsoft C/C++ compiler is 17.00.


1 Answers

This article answers your question:

  • C/C++ tip: How to detect the operating system type using compiler predefined macros (plus archive.org link in case it vanishes).

The article is quite long, and includes tables that are hard to reproduce, but here's the essence:

You can detect Unix-style OS with:

#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))     /* UNIX-style OS. ------------------------------------------- */  #endif 

Once you know it's Unix, you can find if it's POSIX and the POSIX version with:

#include <unistd.h> #if defined(_POSIX_VERSION)     /* POSIX compliant */ #endif 

You can check for BSD-derived systems with:

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) #include <sys/param.h> #if defined(BSD)     /* BSD (DragonFly BSD, FreeBSD, OpenBSD, NetBSD). ----------- */  #endif #endif 

and Linux with:

#if defined(__linux__)     /* Linux  */ #endif 

and Apple's operating systems with

#if defined(__APPLE__) && defined(__MACH__)     /* Apple OSX and iOS (Darwin) */ #include <TargetConditionals.h> #if TARGET_IPHONE_SIMULATOR == 1     /* iOS in Xcode simulator */ #elif TARGET_OS_IPHONE == 1     /* iOS on iPhone, iPad, etc. */     #elif TARGET_OS_MAC == 1     /* OS X */ #endif #endif 

Windows with Cygwin

#if defined(__CYGWIN__) && !defined(_WIN32)     /* Cygwin POSIX under Microsoft Windows. */ #endif 

And non-POSIX Windows with:

#if defined(_WIN64)     /* Microsoft Windows (64-bit) */ #elif defined(_WIN32)     /* Microsoft Windows (32-bit) */ #endif 

The full article lists the following symbols, and shows which systems define them and when: _AIX, __APPLE__, __CYGWIN32__, __CYGWIN__, __DragonFly__, __FreeBSD__, __gnu_linux, hpux, __hpux, linux, __linux, __linux__, __MACH__, __MINGW32__, __MINGW64__, __NetBSD__, __OpenBSD__, _POSIX_IPV6, _POSIX_MAPPED_FILES, _POSIX_SEMAPHORES, _POSIX_THREADS, _POSIX_VERSION, sun, __sun, __SunOS, __sun__, __SVR4, __svr4__, TARGET_IPHONE_SIMULATOR, TARGET_OS_EMBEDDED, TARGET_OS_IPHONE, TARGET_OS_MAC, UNIX, unix, __unix, __unix__, WIN32, _WIN32, __WIN32, __WIN32__, WIN64, _WIN64, __WIN64, __WIN64__, WINNT, __WINNT, __WINNT__.

A related article (archive.org link) covers detecting compilers and compiler versions. It lists the following symbols: __clang__, __GNUC__, __GNUG__, __HP_aCC, __HP_cc, __IBMCPP__, __IBMC__, __ICC, __INTEL_COMPILER, _MSC_VER, __PGI, __SUNPRO_C, __SUNPRO_CC for detecting compilers, and __clang_major__, __clang_minor__, __clang_patchlevel__, __clang_version__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, __GNUC__, __GNUG__, __HP_aCC, __HP_cc, __IBMCPP__, __IBMC__, __ICC, __INTEL_COMPILER, __INTEL_COMPILER_BUILD_DATE, _MSC_BUILD, _MSC_FULL_VER, _MSC_VER, __PGIC_MINOR__, __PGIC_PATCHLEVEL__, __PGIC__, __SUNPRO_C, __SUNPRO_CC, __VERSION__, __xlC_ver__, __xlC__, __xlc__ for detecting compiler versions.

like image 156
Charphacy Avatar answered Oct 01 '22 07:10

Charphacy