ant2009
ant2009

Reputation: 22556

Using macros WIN32 or _MSC_VER cross-platform

I am compiling under Linux (GCC 4.4.2) and Windows VS C++ Express Edition 2008

I am currently compiling under Windows XP Pro 32 bit, and have added this to my source code.

#if defined( WIN32 )
/* Do windows stuff here */
#endif

However, the code in the if statement is disabled (grayed out). However if I do the following:

#if defined( _MSC_VER )
/* Do windows stuff here */
#endif

The if statement code is enabled.

I am just wondering, what should I be using. I have seen many programmers use WIN32. However, doesn't seem to work for me. Should I be using _MSC_VER instead?

Many thanks for any advice,

Upvotes: 13

Views: 10913

Answers (3)

jamesdlin
jamesdlin

Reputation: 90105

There is no WIN32. If you've seen it being used elsewhere, it's either wrong or the code is explicitly defining that macro itself somewhere.

You want _WIN32. See https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros for a list of predefined macros in Microsoft's compiler.

Upvotes: 19

Ryan Mckenna
Ryan Mckenna

Reputation: 104

This worked for me!

#if defined (_WIN32)
#define PLATFORM "Windows"
#elif defined (__linux)
#define PLATFORM "Linux"
#endif
#include <iostream>
using namespace std;

int main()
{
  cout << PLATFORM << "System" << endl;
  return 0;
}

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 942000

Use _WIN32 instead. The IntelliSense parser in VS2008 is troublesome, this might not necessarily solve your problem. It got a complete rewrite in VS2010.

Upvotes: 3

Related Questions