c++ #ifdef Mac OS X question
Asked Answered
P

4

47

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!

Philology answered 23/7, 2011 at 19:49 Comment(0)
D
47

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 
Duelist answered 23/7, 2011 at 19:57 Comment(5)
Of course, #ifdef __APPLE__ would be enough if the OP doesn't target iOS.Fosse
@larsmans: Better be sure then sorry. If the user decides to simplify, sure :)Duelist
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.)Abaft
Also, TARGET_OS_MAC is 1 on the iPhone, so if you want to differentiate between Mac and iPhone, test for iPhone first.Armored
#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 #endifAphasia
J
15

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

Johnsiejohnson answered 23/7, 2011 at 19:58 Comment(0)
O
8

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
Otoplasty answered 1/8, 2014 at 19:55 Comment(0)
D
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.

Dignify answered 1/2, 2015 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.