c ++ #ifdef Mac OS X问题

Kir*_*sty 34 c++ macos compatibility conditional-compilation

我是C++的新手.我目前正在开展一个小组项目,我们希望我们的课程兼容实验室计算机(Windows)和我的计算机(Mac OS X).

以下是我们在文件顶部放置的内容:

#ifdef TARGET_OS_X
#    include <GLUT/glut.h>
#    include <OpenGL/OpenGL.h>
#elif defined _WIN32 || defined _WIN64
#    include <GL\glut.h>
#endif
Run Code Online (Sandbox Code Playgroud)

我意识到之前已经问过这个问题,但是我的搜索给了我相互矛盾的答案,例如"_MAC","TARGET_MAC_OS","MACINTOSH"等.在#ifdef语句中输入的当前和正确的声明是什么兼容Mac?现在它不起作用.

谢谢!

orl*_*rlp 33

根据这个答案:

#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
Run Code Online (Sandbox Code Playgroud)

简而言之:

#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 
Run Code Online (Sandbox Code Playgroud)

  • `TARGET_OS_IPHONE`是为iOS模拟器定义的,所以你需要在它之前放置`TARGET_IPHONE_SIMULATOR`测试.*另外*,这三个都是在Apple的平台上定义的 - 你必须按值测试(`#if TARGET_OS_IPHONE`而不是`#ifdef TARGET_OS_IPHONE`.) (9认同)
  • 此外,iPhone上的"TARGET_OS_MAC"为1,因此如果要区分Mac和iPhone,请首先测试iPhone. (3认同)
  • @larsmans:最好确定对不起.如果用户决定简化,肯定:) (2认同)

koa*_*oan 11

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


Jon*_*cer 8

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

#ifdef __APPLE__
    DoSomething();
#elif _WIN32
    DoSomethingElse();
#else
    GenerateErrorOrIgnore
Run Code Online (Sandbox Code Playgroud)


小智 5

小修正:#ifdef TARGET_OS_MAC在OS X和iOS上都会始终如一,因为它根据平台定义了0或1,但是在定义APPLE时,也定义了TARGET_OS_MAC,因此请在#ifdef APPLE中进行检查一文不值。您可能要使用#if TARGET_OS_MAC。对于所有TARGET_ *宏相同。