11

I can not get the #ifdef rule to work at least on windows (64 bit). Compiler version is g++ 5.4.0 I have tried:

#ifdef _WIN32
#ifdef _WIN64
#ifdef OS_WINDOWS

I have compiled the following test with: g++ main.cpp

Even with a simple code as this:

#include <iostream>

int main()
{
  std::cout << "you are on...";

  #ifdef _WIN32
  std::cout << "Windows" << std::endl;
  #elif __linux__
  std::cout << "Linux" << std::endl;
  #endif

  return 0;
}

Output is:

"you are on..."

...and nothing else gets couted out.

3 Answers3

11
#ifdef _WIN32
#ifdef _WIN64

These are pre-defined macros defined by the MSVC compiler. You appear to be using g++ instead. That probably means either MinGW, or Cygwin.


Here and here are collections of macros pre-defined by several compilers.

MinGW __MINGW32__

Cygwin __CYGWIN__


If you prefer to not to build hefty ifdef - else trees, and scour the internet for macros defined by obscure compilers, and their different versions, I recommend to instead include a few headers from boost. They have already done the hard part of the work. Although, note that BOOST_OS_WINDOWS is separate from BOOST_OS_CYGWIN.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
3

Use __CYGWIN32__ to detect Windows when compiling g++ in cygwin. (This is defined in both 32 and 64 bit).

_WIN32 &c. may not be defined in that case. It isn't for me.

(As also mentioned in a comment; using echo | g++ -dM -E to output the list of what is defined can be helpful.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
-2

yes, it is true that this:

#ifdef _WIN32

sometimes doesn't work on Windows (!) In this case, use the following form instead:

#if defined(_WIN32)

I was surprised but I have seen that problem and it helped me!