13 Replies - 769 Views - Last Post: 07 August 2011 - 07:25 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Such thing as or in #ifdef

Posted 06 August 2011 - 06:32 AM

Is there such thing as an or in an #ifdef line?

Example of my a portion of main function

#define ABC
#ifdef _WIN32_
#undef ABC
#endif
#ifdef _WINDOWS_
#ifdef ABC
#undef ABC
#endif
#endif
#ifdef WINDOWS
#ifdef ABC
#undef ABC
#endif
#endif
#ifdef ABC
std::cout<<"You are using an unfamiliar OS. . ."<<std::endl;
#endif
return 0;


If I know how to have an or command in my #ifdefs, then my code can be simplified to this:
#define ABC
#ifdef _WIN32_ or WIN32 or _WINDOWS_ or WINDOWS
#undef ABC
#endif
#ifdef ABC
std::cout<<"You are using an unfamiliar OS. . ."<<std::endl;
#endif


Thanks for reading

Oh wait. . . I just realized.
This could be my new code:
#ifndef _WIN32_ or WIN32 or _WINDOWS_ or WINDOWS
std::cout<<"You are using an unfamiliar OS. . ."<<std::endl;
#endif


Is This A Good Question/Topic? 1
  • +

Replies To: Such thing as or in #ifdef

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 06:40 AM

I believe that if you use #if then you can use most of the "usual operators" however I can't seem to find much formal documentation on this.

#define TEST2

#if defined TEST || defined TEST2
cout << "made it here" << endl;
#endif

#if defined TEST && defined TEST2
cout << "if this works its broken" << endl;
#endif


you can read up on this on MSDN here and it covers some basics but does not really give much detail about what operators can and can not be used. Apparently you CAN use arithmetic? I always though it was boolean and comparisons.
Was This Post Helpful? 1
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 06:40 AM

#if !(defined _WIN32_ || defined WIN32  || defined _WINDOWS || defined WINDOWS)
    std::cout<<"You are using an unfamiliar OS. . ."<<std::endl;
#endif


Untested, but looks about right.
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 06:43 AM

Note that this doesn't really matter as they are compile-time only, so unless you're writing a header for other people it's irrelevant.
Was This Post Helpful? 1
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 07:13 AM

PlasticineGuy does have a point -- this makes sense to do for compile-time code but not for runtime code really. So if I compile the program on windows and then try to run it on Linux then of course the message will never display... I doubt the program will even launch properly.

Now if someone tried to compile your code on linux and then ran it they would get your message... so there is that.
Was This Post Helpful? 2
  • +
  • -

#6 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 06:03 PM

Seriously? It would not run? Dang. . .

#ifdef
//is this the same as
#if defined

?
Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 07:06 PM

Linux and Windows use different file formats for executables. Not much you can do about that because the Microsoft format is (probably) proprietary.
Was This Post Helpful? 1
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Such thing as or in #ifdef

Posted 06 August 2011 - 08:08 PM

You will have a shock when you hear about Mono then. Same executable runs on both Linux and Windows.

This post has been edited by Oler1s: 06 August 2011 - 08:08 PM

Was This Post Helpful? 1
  • +
  • -

#9 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 03:20 AM

Ok. Is #ifdef the same as #if defined?
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 03:25 AM

Yes.
Was This Post Helpful? 1
  • +
  • -

#11 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 04:56 AM

Then why use #if defined rather than #ifdef? Is it classic?
Was This Post Helpful? 0
  • +
  • -

#12 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 05:00 AM

#ifdef only supports 1 operand whereas #if is essentially like an if() statement for the preprocessor.

If you're only checking if 1 thing is defined it doesn't make a difference.
Was This Post Helpful? 2
  • +
  • -

#13 hulla  Icon User is offline

  • sqrt(-1) am awesome


Reputation: 49
  • View blog
  • Posts: 727
  • Joined: 05-March 11

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 06:09 AM

Ah ok. . . Thank you :)
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2222
  • View blog
  • Posts: 9,208
  • Joined: 18-February 07

Re: Such thing as or in #ifdef

Posted 07 August 2011 - 07:25 AM

*
POPULAR

While there may be cross-platform emulation allowing Linux programs to run on Windows or Windows programs to run on Linux this does not change what happens at compile time.

Using #if/#ifdef/#ifndef etc. are compile time constructs and there is nothing magical about the preprocessor macros WINDOWS, _WIN32 etc. -- They are just preprocessor macros and while they can be used to detect the OS there is no reason why some yahoo can't go and -D them in a makefile.

the code:

#if defined WIN32
cout << "this line is for windows" << endl;
#endif


will insert the cout code into the final executable if the macro WIN32 is defined. If you yahoo add -DWIN32 to the makefile when compiling this on a non-windows platform then the cout line will be added regardless of platform. If some yahoo undefines WIN32 then the line will be omitted regardless of platform.

These macros are either compiler supplied and always defined when using that particular compiler (even when running on a different platform via some emulation) or defined in some header file.
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1