Is it possible to use #ifdef with Boolean AND and OR? For instance something like

#ifdef WIN32 || WIN64
cout << "This is Windows." << endl;
#endif

The above doesn't give a compiler error for me, although it doesn't evaluate the or condition.

The above doesn't give a compiler error for me, although it doesn't evaluate the or condition.

If using a Microsoft compiler you have the wrong macros #ifdef _WIN32 || _WIN64

I'm using MinGW. I checked and WIN32 and _WIN32 both evaluate as true for me.

So this syntax is actually OK? Because the following gives me "warning: extra tokens at end of #ifdef directive" warnings for the lines with the || signs

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]){
	#ifdef WIN32
		printf("This is WIN32.\n");
	#endif
	#ifdef WIN64
		printf("This is WIN64.\n");
	#endif
	#ifdef WIN32 || WIN64
		printf("WIN32 first.\n");
	#endif
	#ifdef WIN64 || WIN32
		printf("WIN64 first.\n");
	#endif
}

And the output is

This is WIN32.
WIN32 first.

So the "#ifdef WIN32 || WIN64" is evaluating as true, but "#ifdef WIN64 || WIN32" is evaluating as false. So I thought that the compiler just skips anything after the || signs.

try this:

#if defined _WIN64 || defined _WIN32
cout << "This is Windows." << endl;
#endif

That works. So I guess #ifdef can't do and/or, you have to use defined. Thanks.

#ifdef is a shorthand form of #if defined . the difference between the two is that '#ifdef' doesnt allow complex expressions.

You can write this like:

#ifdef WIN32
  #define IS_WINDOWS_PLATFORM
#endif
#ifdef WIN64
  #define IS_WINDOWS_PLATFORM
#endif
//so here what you should user after
#ifdef IS_WINDOWS_PLATFORM
//your code for windows
#endif
commented: for bumping 2 year old thread -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.