区分不同操作系统编译器不同版本的宏

Posted braveliu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了区分不同操作系统编译器不同版本的宏相关的知识,希望对你有一定的参考价值。

【1】不同操作系统的宏

可以参考QtCore/qsystemdetection.h文件:

  1 /****************************************************************************
  2 **
  3 ** Copyright (C) 2016 The Qt Company Ltd.
  4 ** Contact: https://www.qt.io/licensing/
  5 **
  6 ** This file is part of the QtCore module of the Qt Toolkit.
  7 **
  8 ** $QT_BEGIN_LICENSE:LGPL$
  9 ** Commercial License Usage
 10 ** Licensees holding valid commercial Qt licenses may use this file in
 11 ** accordance with the commercial license agreement provided with the
 12 ** Software or, alternatively, in accordance with the terms contained in
 13 ** a written agreement between you and The Qt Company. For licensing terms
 14 ** and conditions see https://www.qt.io/terms-conditions. For further
 15 ** information use the contact form at https://www.qt.io/contact-us.
 16 **
 17 ** GNU Lesser General Public License Usage
 18 ** Alternatively, this file may be used under the terms of the GNU Lesser
 19 ** General Public License version 3 as published by the Free Software
 20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
 21 ** packaging of this file. Please review the following information to
 22 ** ensure the GNU Lesser General Public License version 3 requirements
 23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
 24 **
 25 ** GNU General Public License Usage
 26 ** Alternatively, this file may be used under the terms of the GNU
 27 ** General Public License version 2.0 or (at your option) the GNU General
 28 ** Public license version 3 or any later version approved by the KDE Free
 29 ** Qt Foundation. The licenses are as published by the Free Software
 30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
 31 ** included in the packaging of this file. Please review the following
 32 ** information to ensure the GNU General Public License requirements will
 33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
 34 ** https://www.gnu.org/licenses/gpl-3.0.html.
 35 **
 36 ** $QT_END_LICENSE$
 37 **
 38 ****************************************************************************/
 39 
 40 #ifndef QGLOBAL_H
 41 # include <QtCore/qglobal.h>
 42 #endif
 43 
 44 #ifndef QSYSTEMDETECTION_H
 45 #define QSYSTEMDETECTION_H
 46 
 47 /*
 48    The operating system, must be one of: (Q_OS_x)
 49 
 50      DARWIN   - Any Darwin system (macOS, ios, watchOS, tvOS)
 51      MACOS    - macOS
 52      IOS      - iOS
 53      WATCHOS  - watchOS
 54      TVOS     - tvOS
 55      MSDOS    - MS-DOS and Windows
 56      OS2      - OS/2
 57      OS2EMX   - XFree86 on OS/2 (not PM)
 58      WIN32    - Win32 (Windows 2000/XP/Vista/7 and Windows Server 2003/2008)
 59      WINRT    - WinRT (Windows 8 Runtime)
 60      CYGWIN   - Cygwin
 61      SOLARIS  - Sun Solaris
 62      HPUX     - HP-UX
 63      ULTRIX   - DEC Ultrix
 64      LINUX    - Linux [has variants]
 65      FREEBSD  - FreeBSD [has variants]
 66      NETBSD   - NetBSD
 67      OPENBSD  - OpenBSD
 68      BSDI     - BSD/OS
 69      INTERIX  - Interix
 70      IRIX     - SGI Irix
 71      OSF      - HP Tru64 UNIX
 72      SCO      - SCO OpenServer 5
 73      UNIXWARE - UnixWare 7, Open UNIX 8
 74      AIX      - AIX
 75      HURD     - GNU Hurd
 76      DGUX     - DG/UX
 77      RELIANT  - Reliant UNIX
 78      DYNIX    - DYNIX/ptx
 79      QNX      - QNX [has variants]
 80      QNX6     - QNX RTP 6.1
 81      LYNX     - LynxOS
 82      BSD4     - Any BSD 4.4 system
 83      UNIX     - Any UNIX BSD/SYSV system
 84      android  - Android platform
 85      HAIKU    - Haiku
 86 
 87    The following operating systems have variants:
 88      LINUX    - both Q_OS_LINUX and Q_OS_ANDROID are defined when building for Android
 89               - only Q_OS_LINUX is defined if building for other Linux systems
 90      FREEBSD  - Q_OS_FREEBSD is defined only when building for FreeBSD with a BSD userland
 91               - Q_OS_FREEBSD_KERNEL is always defined on FreeBSD, even if the userland is from GNU
 92 */
 93 
 94 #if defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
 95 #  include <TargetConditionals.h>
 96 #  if defined(TARGET_OS_MAC) && TARGET_OS_MAC
 97 #    define Q_OS_DARWIN
 98 #    define Q_OS_BSD4
 99 #    ifdef __LP64__
100 #      define Q_OS_DARWIN64
101 #    else
102 #      define Q_OS_DARWIN32
103 #    endif
104 #    if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
105 #      define QT_PLATFORM_UIKIT
106 #      if defined(TARGET_OS_WATCH) && TARGET_OS_WATCH
107 #        define Q_OS_WATCHOS
108 #      elif defined(TARGET_OS_TV) && TARGET_OS_TV
109 #        define Q_OS_TVOS
110 #      else
111 #        // TARGET_OS_IOS is only available in newer SDKs,
112 #        // so assume any other iOS-based platform is iOS for now
113 #        define Q_OS_IOS
114 #      endif
115 #    else
116 #      // TARGET_OS_OSX is only available in newer SDKs,
117 #      // so assume any non iOS-based platform is macOS for now
118 #      define Q_OS_MACOS
119 #    endif
120 #  else
121 #    error "Qt has not been ported to this Apple platform - see http://www.qt.io/developers"
122 #  endif
123 #elif defined(__ANDROID__) || defined(ANDROID)
124 #  define Q_OS_ANDROID
125 #  define Q_OS_LINUX
126 #elif defined(__CYGWIN__)
127 #  define Q_OS_CYGWIN
128 #elif !defined(SAG_COM) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY==WINAPI_FAMILY_DESKTOP_APP) && (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))
129 #  define Q_OS_WIN32
130 #  define Q_OS_WIN64
131 #elif !defined(SAG_COM) && (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
132 #  if defined(WINAPI_FAMILY)
133 #    ifndef WINAPI_FAMILY_PC_APP
134 #      define WINAPI_FAMILY_PC_APP WINAPI_FAMILY_APP
135 #    endif
136 #    if defined(WINAPI_FAMILY_PHONE_APP) && WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP
137 #      define Q_OS_WINRT
138 #    elif WINAPI_FAMILY==WINAPI_FAMILY_PC_APP
139 #      define Q_OS_WINRT
140 #    else
141 #      define Q_OS_WIN32
142 #    endif
143 #  else
144 #    define Q_OS_WIN32
145 #  endif
146 #elif defined(__sun) || defined(sun)
147 #  define Q_OS_SOLARIS
148 #elif defined(hpux) || defined(__hpux)
149 #  define Q_OS_HPUX
150 #elif defined(__ultrix) || defined(ultrix)
151 #  define Q_OS_ULTRIX
152 #elif defined(sinix)
153 #  define Q_OS_RELIANT
154 #elif defined(__native_client__)
155 #  define Q_OS_NACL
156 #elif defined(__linux__) || defined(__linux)
157 #  define Q_OS_LINUX
158 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
159 #  ifndef __FreeBSD_kernel__
160 #    define Q_OS_FREEBSD
161 #  endif
162 #  define Q_OS_FREEBSD_KERNEL
163 #  define Q_OS_BSD4
164 #elif defined(__NetBSD__)
165 #  define Q_OS_NETBSD
166 #  define Q_OS_BSD4
167 #elif defined(__OpenBSD__)
168 #  define Q_OS_OPENBSD
169 #  define Q_OS_BSD4
170 #elif defined(__bsdi__)
171 #  define Q_OS_BSDI
172 #  define Q_OS_BSD4
173 #elif defined(__INTERIX)
174 #  define Q_OS_INTERIX
175 #  define Q_OS_BSD4
176 #elif defined(__sgi)
177 #  define Q_OS_IRIX
178 #elif defined(__osf__)
179 #  define Q_OS_OSF
180 #elif defined(_AIX)
181 #  define Q_OS_AIX
182 #elif defined(__Lynx__)
183 #  define Q_OS_LYNX
184 #elif defined(__GNU__)
185 #  define Q_OS_HURD
186 #elif defined(__DGUX__)
187 #  define Q_OS_DGUX
188 #elif defined(__QNXNTO__)
189 #  define Q_OS_QNX
190 #elif defined(_SEQUENT_)
191 #  define Q_OS_DYNIX
192 #elif defined(_SCO_DS) /* SCO OpenServer 5 + GCC */
193 #  define Q_OS_SCO
194 #elif defined(__USLC__) /* all SCO platforms + UDK or OUDK */
195 #  define Q_OS_UNIXWARE
196 #elif defined(__svr4__) && defined(i386) /* Open UNIX 8 + GCC */
197 #  define Q_OS_UNIXWARE
198 #elif defined(__INTEGRITY)
199 #  define Q_OS_INTEGRITY
200 #elif defined(VXWORKS) /* there is no "real" VxWorks define - this has to be set in the mkspec! */
201 #  define Q_OS_VXWORKS
202 #elif defined(__HAIKU__)
203 #  define Q_OS_HAIKU
204 #elif defined(__MAKEDEPEND__)
205 #else
206 #  error "Qt has not been ported to this OS - see http://www.qt-project.org/"
207 #endif
208 
209 #if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) || defined(Q_OS_WINRT)
210 #  define Q_OS_WIN
211 #endif
212 
213 #if defined(Q_OS_WIN)
214 #  undef Q_OS_UNIX
215 #elif !defined(Q_OS_UNIX)
216 #  define Q_OS_UNIX
217 #endif
218 
219 // Compatibility synonyms
220 #ifdef Q_OS_DARWIN
221 #define Q_OS_MAC
222 #endif
223 #ifdef Q_OS_DARWIN32
224 #define Q_OS_MAC32
225 #endif
226 #ifdef Q_OS_DARWIN64
227 #define Q_OS_MAC64
228 #endif
229 #ifdef Q_OS_MACOS
230 #define Q_OS_MACX
231 #define Q_OS_OSX
232 #endif
233 
234 #ifdef Q_OS_DARWIN
235 #  include <Availability.h>
236 #  include <AvailabilityMacros.h>
237 #
238 #  ifdef Q_OS_MACOS
239 #    if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_6
240 #       undef __MAC_OS_X_VERSION_MIN_REQUIRED
241 #       define __MAC_OS_X_VERSION_MIN_REQUIRED __MAC_10_6
242 #    endif
243 #    if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
244 #       undef MAC_OS_X_VERSION_MIN_REQUIRED
245 #       define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
246 #    endif
247 #  endif
248 #
249 #  // Numerical checks are preferred to named checks, but to be safe
250 #  // we define the missing version names in case Qt uses them.
251 #
252 #  if !defined(__MAC_10_7)
253 #       define __MAC_10_7 1070
254 #  endif
255 #  if !defined(__MAC_10_8)
256 #       define __MAC_10_8 1080
257 #  endif
258 #  if !defined(__MAC_10_9)
259 #       define __MAC_10_9 1090
260 #  endif
261 #  if !defined(__MAC_10_10)
262 #       define __MAC_10_10 101000
263 #  endif
264 #  if !defined(__MAC_10_11)
265 #       define __MAC_10_11 101100
266 #  endif
267 #  if !defined(__MAC_10_12)
268 #       define __MAC_10_12 101200
269 #  endif
270 #  if !defined(__MAC_10_13)
271 #       define __MAC_10_13 101300
272 #  endif
273 #  if !defined(MAC_OS_X_VERSION_10_7)
274 #       define MAC_OS_X_VERSION_10_7 1070
275 #  endif
276 #  if !defined(MAC_OS_X_VERSION_10_8)
277 #       define MAC_OS_X_VERSION_10_8 1080
278 #  endif
279 #  if !defined(MAC_OS_X_VERSION_10_9)
280 #       define MAC_OS_X_VERSION_10_9 1090
281 #  endif
282 #  if !defined(MAC_OS_X_VERSION_10_10)
283 #       define MAC_OS_X_VERSION_10_10 101000
284 #  endif
285 #  if !defined(MAC_OS_X_VERSION_10_11)
286 #       define MAC_OS_X_VERSION_10_11 101100
287 #  endif
288 #  if !defined(MAC_OS_X_VERSION_10_12)
289 #       define MAC_OS_X_VERSION_10_12 101200
290 #  endif
291 #  if !defined(MAC_OS_X_VERSION_10_13)
292 #       define MAC_OS_X_VERSION_10_13 101300
293 #  endif
294 #
295 #  if !defined(__IPHONE_4_3)
296 #       define __IPHONE_4_3 40300
297 #  endif
298 #  if !defined(__IPHONE_5_0)
299 #       define __IPHONE_5_0 50000
300 #  endif
301 #  if !defined(__IPHONE_5_1)
302 #       define __IPHONE_5_1 50100
303 #  endif
304 #  if !defined(__IPHONE_6_0)
305 #       define __IPHONE_6_0 60000
306 #  endif
307 #  if !defined(__IPHONE_6_1)
308 #       define __IPHONE_6_1 60100
309 #  endif
310 #  if !defined(__IPHONE_7_0)
311 #       define __IPHONE_7_0 70000
312 #  endif
313 #  if !defined(__IPHONE_7_1)
314 #       define __IPHONE_7_1 70100
315 #  endif
316 #  if !defined(__IPHONE_8_0)
317 #       define __IPHONE_8_0 80000
318 #  endif
319 #  if !defined(__IPHONE_8_1)
320 #       define __IPHONE_8_1 80100
321 #  endif
322 #  if !defined(__IPHONE_8_2)
323 #       define __IPHONE_8_2 80200
324 #  endif
325 #  if !defined(__IPHONE_8_3)
326 #       define __IPHONE_8_3 80300
327 #  endif
328 #  if !defined(__IPHONE_8_4)
329 #       define __IPHONE_8_4 80400
330 #  endif
331 #  if !defined(__IPHONE_9_0)
332 #       define __IPHONE_9_0 90000
333 #  endif
334 #  if !defined(__IPHONE_9_1)
335 #       define __IPHONE_9_1 90100
336 #  endif
337 #  if !defined(__IPHONE_9_2)
338 #       define __IPHONE_9_2 90200
339 #  endif
340 #  if !defined(__IPHONE_9_3)
341 #       define __IPHONE_9_3 90300
342 #  endif
343 #  if !defined(__IPHONE_10_0)
344 #       define __IPHONE_10_0 100000
345 #  endif
346 #  if !defined(__IPHONE_10_1)
347 #       define __IPHONE_10_1 100100
348 #  endif
349 #  if !defined(__IPHONE_10_2)
350 #       define __IPHONE_10_2 100200
351 #  endif
352 #  if !defined(__IPHONE_10_3)
353 #       define __IPHONE_10_3 100300
354 #  endif
355 #  if !defined(__IPHONE_11_0)
356 #       define __IPHONE_11_0 110000
357 #  endif
358 #endif
359 
360 #ifdef __LSB_VERSION__
361 #  if __LSB_VERSION__ < 40
362 #    error "This version of the Linux Standard Base is unsupported"
363 #  endif
364 #ifndef QT_LINUXBASE
365 #  define QT_LINUXBASE
366 #endif
367 #endif
368 
369 #endif // QSYSTEMDETECTION_H

各种操作系统的宏定义。

【2】不同编译器不同版本的宏

(1)不同编译器

1.1 Visual C++ 编译器预定义宏:C/C++ Predefined Macros

1.2 GCC 编译器预定义宏:GNU Common Predefined Macros

(2)不同版本

2.1 Visual C++ 编译器 _MSC_VER

_MSC_VER 即时Microsoft C/C++ 编译器版本预定义宏。

一些编译器版本的_MSC_VER值:

 1 MS VC++ 14.0 _MSC_VER = 1900 (Visual Studio 2015)
 2 MS VC++ 12.0 _MSC_VER = 1800 (VisualStudio 2013)
 3 MS VC++ 11.0 _MSC_VER = 1700 (VisualStudio 2012)
 4 MS VC++ 10.0 _MSC_VER = 1600(VisualStudio 2010)
 5 MS VC++ 9.0 _MSC_VER = 1500(VisualStudio 2008)
 6 MS VC++ 8.0 _MSC_VER = 1400(VisualStudio 2005)
 7 MS VC++ 7.1 _MSC_VER = 1310(VisualStudio 2003)
 8 MS VC++ 7.0 _MSC_VER = 1300(VisualStudio .NET)
 9 MS VC++ 6.0 _MSC_VER = 1200(VisualStudio 98)
10 MS VC++ 5.0 _MSC_VER = 1100(VisualStudio 97)
11 其中MS VC++ 14.0表示Visual C++的版本为14.0,后面括号中的Visual Studio 2015,表明该VC++包含在微软开发工具Visual Studio 2015中。

其应用示例如下:

 1 #if _MSC_VER >= 1400 // for vc8, or vc9
 2 #ifdef _DEBUG
 3 #pragma comment( lib, "SomeLib-vc8-d.lib" )
 4 #elif
 5 #pragma comment( lib, "SomeLib-vc8-r.lib" )
 6 #endif
 7 #elif _MSC_VER >= 1310 // for vc71
 8 #ifdef _DEBUG
 9 #pragma comment( lib, "SomeLib-vc71-d.lib" )
10 #elif
11 #pragma comment( lib, "SomeLib-vc71-r.lib" )
12 #endif
13 #elif _MSC_VER >=1200 // for vc6
14 #ifdef _DEBUG
15 #pragma comment( lib, "SomeLib-vc6-d.lib" )
16 #elif
17 #pragma comment( lib, "SomeLib-vc6-r.lib" )
18 #endif
19 #endif

如上所述。

2.2 GCC编译器 _GNUC_

__GNUC__ 、__GNUC_MINOR__ 、__GNUC_PATCHLEVEL__分别代表gcc的主版本号,次版本号,修正版本号。

应用示例:比如我们的代码要求gcc的版本至少在3.2.0以上,我们就可以写成如下方式的条件编译:

1 /* Test for GCC > 3.2.0 */
2 #if __GNUC__ > 3 || 3   (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || 4     (__GNUC_MINOR__ == 2 && 5       __GNUC_PATCHLEVEL__ > 0)))
6   printf("gcc > 3.2.0
");
7   //...
8 #endif

 

Good Good Study, Day Day Up.

顺序 选择 循环 总结 

以上是关于区分不同操作系统编译器不同版本的宏的主要内容,如果未能解决你的问题,请参考以下文章

cocos creator 2.1版本Material材质系统解析

GCC/MingW 在不同版本上编译

相同的源代码,不同的可执行文件大小?

如何有条件地将 C 代码片段编译到我的 Perl 模块?

支持动态或静态片段的不同屏幕尺寸?

32 bit 与 64 bit 程序比较