42

I am fairly new to C++. I am currently working on a group project and we want to make our classes compatible with both the lab computers (Windows) and my computer (Mac OS X).

Here is what we have been putting at the top of our files:

#ifdef TARGET_OS_X
#    include <GLUT/glut.h>
#    include <OpenGL/OpenGL.h>
#elif defined _WIN32 || defined _WIN64
#    include <GL\glut.h>
#endif

I realize this question has been asked before but my searches have been giving me conflicting answers such as "_MAC", "TARGET_MAC_OS", "MACINTOSH", etc. What is the current and correct declaration to put in the #ifdef statement to make this compatible with Mac? Right now it is not working.

Thank you!

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
Kirsty
  • 421
  • 1
  • 4
  • 3

4 Answers4

43

According to this answer:

#ifdef __APPLE__
    #include "TargetConditionals.h"
    #ifdef TARGET_OS_IPHONE
         // iOS
    #elif TARGET_IPHONE_SIMULATOR
        // iOS Simulator
    #elif TARGET_OS_MAC
        // Other kinds of Mac OS
    #else
        // Unsupported platform
    #endif
#endif

So in short:

#ifdef __APPLE__
    #include "TargetConditionals.h"
    #ifdef TARGET_OS_MAC
        #include <GLUT/glut.h>
        #include <OpenGL/OpenGL.h>
    #endif
#elif defined _WIN32 || defined _WIN64
    #include <GL\glut.h>
#endif 
Community
  • 1
  • 1
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    Of course, `#ifdef __APPLE__` would be enough if the OP doesn't target iOS. – Fred Foo Jul 23 '11 at 20:02
  • 2
    @larsmans: Better be sure then sorry. If the user decides to simplify, sure :) – orlp Jul 23 '11 at 20:04
  • 11
    `TARGET_OS_IPHONE` is defined for the iOS Simulator, so you'll need to put the `TARGET_IPHONE_SIMULATOR` test before it. *Also*, all three are defined on Apple's platforms--you must test by value (`#if TARGET_OS_IPHONE` rather than `#ifdef TARGET_OS_IPHONE`.) – Jonathan Grynspan Jul 23 '11 at 20:52
  • 4
    Also, `TARGET_OS_MAC` is 1 on the iPhone, so if you want to differentiate between Mac and iPhone, test for iPhone first. – Jakob Egger Dec 25 '12 at 08:09
  • 1
    `#ifdef` for the define with 0 assigned? Just look at TargetConditionals.h: for APPLE we have both IPHONE and MAC defined. That is misleading. What I do is: `#ifdef __APPLE__ #include "TargetConditionals.h" #if(TARGET_OS_MAC) #define RUNNING_ON_MAC #endif #endif` – Alexander V Oct 31 '16 at 20:37
15

It depends on the compiler. #ifdef __APPLE__ works for gcc.

koan
  • 3,596
  • 2
  • 25
  • 35
7

According to Microsoft, _WIN32 will cover both the 32-bit and 64-bit versions of Windows. And __APPLE__ works for Clang (at least in Mavericks). So one correct way to write the ifdefs above is:

#ifdef __APPLE__
    DoSomething();
#elif _WIN32
    DoSomethingElse();
#else
    GenerateErrorOrIgnore
Jon Spencer
  • 569
  • 5
  • 8
6

Small correction: #ifdef TARGET_OS_MAC will get you always true on both OS X and iOS, as it is defines either 0 or 1 depending on platform, but when APPLE is defined, TARGET_OS_MAC is defined as well, so checking it inside the #ifdef APPLE is worthless. You might want to use #if TARGET_OS_MAC instead. Same for all TARGET_* macros.

Respiro
  • 61
  • 1
  • 1