c ++ #ifdef Mac OS X 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6802903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:41:56  来源:igfitidea点击:

c++ #ifdef Mac OS X question

c++macoscompatibilityconditional-compilation

提问by Kirsty

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).

我对 C++ 相当陌生。我目前正在进行一个小组项目,我们希望我们的课程与实验室计算机 (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? Right now it is not working.

我意识到以前有人问过这个问题,但我的搜索给了我相互矛盾的答案,例如“_MAC”、“TARGET_MAC_OS”、“MACINTOSH”等。在#ifdef 语句中放入的当前和正确的声明是什么来实现这一点与Mac兼容?现在它不起作用。

Thank you!

谢谢!

回答by orlp

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 

回答by koan

It depends on the compiler. #ifdef __APPLE__works for gcc.

这取决于编译器。#ifdef __APPLE__适用于 gcc。

回答by Respiro

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 APPLEis defined, TARGET_OS_MAC is defined as well, so checking it inside the #ifdef APPLEis worthless. You might want to use #if TARGET_OS_MAC instead. Same for all TARGET_* macros.

小更正:#ifdef TARGET_OS_MAC 将使您在 OS X 和 iOS 上始终为真,因为它根据平台定义了 0 或 1,但是当定义了APPLE 时,也定义了 TARGET_OS_MAC,因此在 #ifdef APPLE 中检查它毫无价值。您可能想改用 #if TARGET_OS_MAC。所有 TARGET_* 宏都相同。

回答by Jon Spencer

According to Microsoft, _WIN32will 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:

据微软称,_WIN32将涵盖 32 位和 64 位版本的 Windows。并__APPLE__为 Clang 工作(至少在小牛队)。因此,编写上述 ifdef 的一种正确方法是:

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