Kirsty
Kirsty

Reputation: 471

c++ #ifdef Mac OS X question

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!

Upvotes: 47

Views: 65083

Answers (4)

Respiro
Respiro

Reputation: 61

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.

Upvotes: 6

Jon Spencer
Jon Spencer

Reputation: 609

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

Upvotes: 9

koan
koan

Reputation: 3686

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

Upvotes: 16

orlp
orlp

Reputation: 117771

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 

Upvotes: 47

Related Questions