如何摆脱未定义预编译器定义的警告

How to get rid of warnings that precompiler definitions are not definied

我从 Unity 网站下载了一个文件

#pragma once

// Standard base includes, defines that indicate our current platform, etc.

#include <stddef.h>


// Which platform we are on?
// UNITY_WIN - Windows (regular win32)
// UNITY_OSX - Mac OS X
// UNITY_LINUX - Linux
// UNITY_IOS - iOS
// UNITY_TVOS - tvOS
// UNITY_ANDROID - Android
// UNITY_METRO - WSA or UWP
// UNITY_WEBGL - WebGL

#if _MSC_VER
    #define UNITY_WIN 1
#elif defined(__APPLE__)
    #if TARGET_OS_TV //'TARGET_OS_TV' is not defined, evaluates to 0
        #define UNITY_TVOS 1
    #elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
        #define UNITY_IOS 1
    #else
        #define UNITY_OSX 1 //'UNITY_OSX' macro redefined
    #endif
#elif defined(__ANDROID__)
    #define UNITY_ANDROID 1
#elif defined(UNITY_METRO) || defined(UNITY_LINUX) || defined(UNITY_WEBGL)
    // these are defined externally
#elif defined(__EMSCRIPTEN__)
    // this is already defined in Unity 5.6
    #define UNITY_WEBGL 1
#else
    #error "Unknown platform!"
#endif

...

问题是,当我尝试将文件包含在我的 XCode 项目中时,我收到了警告(将它们放在评论中)

...
    #if TARGET_OS_TV //'TARGET_OS_TV' is not defined, evaluates to 0
        #define UNITY_TVOS 1
    #elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
        #define UNITY_IOS 1
    #else
        #define UNITY_OSX 1 //'UNITY_OSX' macro redefined
    #endif
...

我尝试使用 #pragma warning(suppress: 4101) 和一些类似的方法,但没有帮助

UPD

...
#ifdef TARGET_OS_TV
        #define UNITY_TVOS 1
    #elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
        #define UNITY_IOS 1
    #else
        #define UNITY_OSX 1 
    #endif
...

使用 ifdef 有助于消除第一个警告,但第二个警告仍然存在

UPD2

...
    #ifdef TARGET_OS_TV
        #define UNITY_TVOS 1
    #elifdef TARGET_OS_IOS //Invalid preprocessing directive
        #define UNITY_IOS 1
    #else
        #define UNITY_OSX 1
    #endif
...

您不应使用 #if 来测试未定义的宏。该警告暗示您应该改用 #ifdef

您不能定义以前定义的宏。您可以先取消定义旧定义,但这很少是个好主意。

Using ifdef helps to get rid of the first warning, but the second one is still in place

在 c++23 中,您将能够使用 #elifdef

否则,您可以使用#elif defined(the_macro)