简体   繁体   English

c ++ #ifdef Mac OS X 问题

[英]c++ #ifdef Mac OS X question

I am fairly new to C++.我对 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).我目前正在进行一个小组项目,我们希望我们的课程与实验室计算机 (Windows) 和我的计算机 (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?我意识到以前有人问过这个问题,但我的搜索一直给我提供相互矛盾的答案,例如“_MAC”、“TARGET_MAC_OS”、“MACINTOSH”等。在#ifdef 语句中放入的当前和正确的声明是什么来实现这一点与Mac兼容? Right now it is not working.现在它不起作用。

Thank you!谢谢!

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 

It depends on the compiler.这取决于编译器。 #ifdef __APPLE__ works for gcc. #ifdef __APPLE__适用于 gcc。

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.小更正:#ifdef TARGET_OS_MAC 将使您在 OS X 和 iOS 上始终为真,因为它根据平台定义了 0 或 1,但是当定义了APPLE时,TARGET_OS_MAC 也被定义了,因此在 #ifdef APPLE 中检查它毫无价值。 You might want to use #if TARGET_OS_MAC instead.您可能想改用 #if TARGET_OS_MAC。 Same for all TARGET_* macros.所有 TARGET_* 宏都相同。

According to Microsoft, _WIN32 will cover both the 32-bit and 64-bit versions of Windows.据微软称, _WIN32将涵盖 32 位和 64 位版本的 Windows。 And __APPLE__ works for Clang (at least in Mavericks). __APPLE__适用于 Clang(至少在小牛队)。 So one correct way to write the ifdefs above is:因此,编写上述 ifdef 的一种正确方法是:

#ifdef __APPLE__
    DoSomething();
#elif _WIN32
    DoSomethingElse();
#else
    GenerateErrorOrIgnore

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM