因为 C++ 具有跨平台的特性,所以有些需求一套代码就多端使用,比如我最近在学习的 OpenGL ES。

但是,不同平台还是具有一定差异性,所以我们首先得判断出是什么平台? 比如 iOS 系统和 Android 系统。

那么如何判断呢?我们接着往下看!

要检查 C 或 C 代码中主机的操作系统,我们需要检查编译器(GNU GCC 或 G )定义的宏。 例如,在 Windows 平台上,编译器定义了一个名为 _WIN32 的特殊宏。 因此,如果定义了宏 _WIN32,我们就在 Windows 上。 同样,其他操作系统也有编译器定义的特定宏。

C++ 编译器预定义了某些全局标识符,称为 manifest constants 。大多数全局标识符以两个下划线 (__) 开头和结尾。

检查 Windows 操作系统的示例:

 
  1. #ifdef _WIN32
  2. printf("You have Windows Operating System");
  3. #endif

1.1 宏定义列表

以下是基于操作系统定义的宏列表:

操作系统宏定义说明
Windows 32 bit + 64 bit_WIN32for all Windows OS
Windows 64 bit_WIN64Only for 64 bit Windows
Apple__APPLE__for all Apple OS
Apple__MACH__alternative to above
iOS embeddedTARGET_OS_EMBEDDEDinclude TargetConditionals.h
iOS stimulatorTARGET_IPHONE_SIMULATORinclude TargetConditionals.h
iPhoneTARGET_OS_IPHONEinclude TargetConditionals.h
MacOSTARGET_OS_MACinclude TargetConditionals.h
Android__ANDROID__subset of linux
Unix based OS__unix__
Linux__linux__subset of unix
POSIX based_POSIX_VERSIONWindows with Cygwin
Solaris__sun
HP UX__hpux
BSDBSDall BSD flavors
DragonFly BSD__DragonFly__
FreeBSD__FreeBSD__
NetBSD__NetBSD__
OpenBSD__OpenBSD__

请注意,宏对 GNU GCC 和 G++ 有效,并且可能因其他编译器而异。 我们将通过一些基本示例,并探讨这些功能在现实生活中的使用。

关于更多的宏定义可以参考下面的两个链接:

1.2 示例: 检测 64 位 Windows 操作系统或 32 位 Windows 操作系统

在下面的示例中,我们专注于检测我们正在运行的 Windows 的风格,它可以是 64 位或 32 位。对于 Windows,我们的表格将是:

操作系统宏定义
Windows OS 32 bit + 64 bit_WIN32
Windows OS 64 bit_WIN64

由于 _WIN32 在 32 位和 64 位 Windows 操作系统中都存在,
所以我们需要先检查 _WIN32 的存在以确认它是 Windows 操作系统,
然后再检查 _WIN64 的存在以确认它是否是 64 位 Windows 操作系统或 32 位 Windows 操作系统。

以下是检查您的 Windows 操作系统的代码:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. #ifdef _WIN32 // Includes both 32 bit and 64 bit
  5. #ifdef _WIN64
  6. printf("Windows 64 bit\n");
  7. #else
  8. printf("Windows 32 bit\n");
  9. #endif
  10. #else
  11. printf("Not a Windows OS\n");
  12. #endif
  13. return 0;
  14. }

运行输出

  1. Windows 32 bit

1.3 示例:检测苹果操作系统是MacOS 还是 iPhone

在此示例中,我们使用 Apple OS 的宏来检测正在使用的 Apple OS,如 MacOS 或 iPhone

  1. #include <stdio.h>
  2. int main()
  3. {
  4. #if __APPLE__
  5. #include "TargetConditionals.h"
  6. #if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
  7. printf("iPhone stimulator\n");
  8. #elif TARGET_OS_IPHONE
  9. printf("iPhone\n");
  10. #elif TARGET_OS_MAC
  11. printf("MacOS\n");
  12. #else
  13. printf("Other Apple OS\n");
  14. #endif
  15. #else
  16. printf("Not an Apple OS\n");
  17. #endif
  18. return 0;
  19. }

运行输出

  1. MacOS

1.4 普通示例

  1. #include <stdio.h>
  2. int main() {
  3. #ifdef _WIN32
  4. printf("Windows\n");
  5. #elif __linux__
  6. printf("Linux\n");
  7. #elif __unix__
  8. printf("Other unix OS\n");
  9. #else
  10. printf("Unidentified OS\n");
  11. #endif
  12. return 0;
  13. }

1.5 作用

凭借检测语言(在我们的案例中为 C 和 C++)中的操作系统的能力,我们可以编写一个跨平台代码,通过分离平台相关代码来在所有平台上运行。

  1. #include <stdio.h>
  2. int main()
  3. {
  4. #if __APPLE__
  5. // apple specific code
  6. #elif _WIN32
  7. // windows specific code
  8. #elif __LINUX__
  9. // linux specific code
  10. #elif BSD
  11. // BSD specific code
  12. #else
  13. // general code or warning
  14. #endif
  15. // general code
  16. return 0;
  17. }

同时,我们可以编写针对特定平台优化的代码。

例如,一个函数调用可能在所有平台上都受支持,但我们可以针对特定平台(例如 Linux)对其进行大幅优化,但是这个新代码会在其他平台上引发错误。 在这种情况下,我们可以使用宏来检测它是否是 Linux,对于这种情况,我们可以轻松地使用其他替代优化代码。

例如:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. #if __linux__
  5. // linux optimized code (will fail in other platforms)
  6. #else
  7. // general code for all platforms
  8. #endif
  9. // general code
  10. return 0;
  11. }

2.1 一个简单的判断

  1. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
  2. //define something for Windows (32-bit and 64-bit, this part is common)
  3. #ifdef _WIN64
  4. //define something for Windows (64-bit only)
  5. #else
  6. //define something for Windows (32-bit only)
  7. #endif
  8. #elif __APPLE__
  9. #include <TargetConditionals.h>
  10. #if TARGET_IPHONE_SIMULATOR
  11. // iOS, tvOS, or watchOS Simulator
  12. #elif TARGET_OS_MACCATALYST
  13. // Mac's Catalyst (ports iOS API into Mac, like UIKit).
  14. #elif TARGET_OS_IPHONE
  15. // iOS, tvOS, or watchOS device
  16. #elif TARGET_OS_MAC
  17. // Other kinds of Apple platforms
  18. #else
  19. # error "Unknown Apple platform"
  20. #endif
  21. #elif __linux__
  22. // linux
  23. #elif __unix__ // all unices not caught above
  24. // Unix
  25. #elif defined(_POSIX_VERSION)
  26. // POSIX
  27. #else
  28. # error "Unknown compiler"
  29. #endif

2.2 优秀的 googletest 的示例

googletest/googletest/include/gtest/internal/gtest-port-arch.h at main · google/googletest · GitHub

  1. // Copyright 2015, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // The Google C++ Testing and Mocking Framework (Google Test)
  30. //
  31. // This header file defines the GTEST_OS_* macro.
  32. // It is separate from gtest-port.h so that custom/gtest-port.h can include it.
  33. #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_
  34. #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_
  35. // Determines the platform on which Google Test is compiled.
  36. #ifdef __CYGWIN__
  37. #define GTEST_OS_CYGWIN 1
  38. #elif defined(__MINGW__) || defined(__MINGW32__) || defined(__MINGW64__)
  39. #define GTEST_OS_WINDOWS_MINGW 1
  40. #define GTEST_OS_WINDOWS 1
  41. #elif defined _WIN32
  42. #define GTEST_OS_WINDOWS 1
  43. #ifdef _WIN32_WCE
  44. #define GTEST_OS_WINDOWS_MOBILE 1
  45. #elif defined(WINAPI_FAMILY)
  46. #include <winapifamily.h>
  47. #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
  48. #define GTEST_OS_WINDOWS_DESKTOP 1
  49. #elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
  50. #define GTEST_OS_WINDOWS_PHONE 1
  51. #elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
  52. #define GTEST_OS_WINDOWS_RT 1
  53. #elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_TV_TITLE)
  54. #define GTEST_OS_WINDOWS_PHONE 1
  55. #define GTEST_OS_WINDOWS_TV_TITLE 1
  56. #else
  57. // WINAPI_FAMILY defined but no known partition matched.
  58. // Default to desktop.
  59. #define GTEST_OS_WINDOWS_DESKTOP 1
  60. #endif
  61. #else
  62. #define GTEST_OS_WINDOWS_DESKTOP 1
  63. #endif // _WIN32_WCE
  64. #elif defined __OS2__
  65. #define GTEST_OS_OS2 1
  66. #elif defined __APPLE__
  67. #define GTEST_OS_MAC 1
  68. #include <TargetConditionals.h>
  69. #if TARGET_OS_IPHONE
  70. #define GTEST_OS_IOS 1
  71. #endif
  72. #elif defined __DragonFly__
  73. #define GTEST_OS_DRAGONFLY 1
  74. #elif defined __FreeBSD__
  75. #define GTEST_OS_FREEBSD 1
  76. #elif defined __Fuchsia__
  77. #define GTEST_OS_FUCHSIA 1
  78. #elif defined(__GNU__)
  79. #define GTEST_OS_GNU_HURD 1
  80. #elif defined(__GLIBC__) && defined(__FreeBSD_kernel__)
  81. #define GTEST_OS_GNU_KFREEBSD 1
  82. #elif defined __linux__
  83. #define GTEST_OS_LINUX 1
  84. #if defined __ANDROID__
  85. #define GTEST_OS_LINUX_ANDROID 1
  86. #endif
  87. #elif defined __MVS__
  88. #define GTEST_OS_ZOS 1
  89. #elif defined(__sun) && defined(__SVR4)
  90. #define GTEST_OS_SOLARIS 1
  91. #elif defined(_AIX)
  92. #define GTEST_OS_AIX 1
  93. #elif defined(__hpux)
  94. #define GTEST_OS_HPUX 1
  95. #elif defined __native_client__
  96. #define GTEST_OS_NACL 1
  97. #elif defined __NetBSD__
  98. #define GTEST_OS_NETBSD 1
  99. #elif defined __OpenBSD__
  100. #define GTEST_OS_OPENBSD 1
  101. #elif defined __QNX__
  102. #define GTEST_OS_QNX 1
  103. #elif defined(__HAIKU__)
  104. #define GTEST_OS_HAIKU 1
  105. #elif defined ESP8266
  106. #define GTEST_OS_ESP8266 1
  107. #elif defined ESP32
  108. #define GTEST_OS_ESP32 1
  109. #elif defined(__XTENSA__)
  110. #define GTEST_OS_XTENSA 1
  111. #endif // __CYGWIN__
  112. #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部