【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 # include42 #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 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_DARWIN64101 # else102 # define Q_OS_DARWIN32103 # endif104 # if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE105 # define QT_PLATFORM_UIKIT106 # if defined(TARGET_OS_WATCH) && TARGET_OS_WATCH107 # define Q_OS_WATCHOS108 # elif defined(TARGET_OS_TV) && TARGET_OS_TV109 # define Q_OS_TVOS110 # else111 # // TARGET_OS_IOS is only available in newer SDKs,112 # // so assume any other iOS-based platform is iOS for now113 # define Q_OS_IOS114 # endif115 # else116 # // TARGET_OS_OSX is only available in newer SDKs,117 # // so assume any non iOS-based platform is macOS for now118 # define Q_OS_MACOS119 # endif120 # else121 # error "Qt has not been ported to this Apple platform - see http://www.qt.io/developers"122 # endif123 #elif defined(__ANDROID__) || defined(ANDROID)124 # define Q_OS_ANDROID125 # define Q_OS_LINUX126 #elif defined(__CYGWIN__)127 # define Q_OS_CYGWIN128 #elif !defined(SAG_COM) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY==WINAPI_FAMILY_DESKTOP_APP) && (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))129 # define Q_OS_WIN32130 # define Q_OS_WIN64131 #elif !defined(SAG_COM) && (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))132 # if defined(WINAPI_FAMILY)133 # ifndef WINAPI_FAMILY_PC_APP134 # define WINAPI_FAMILY_PC_APP WINAPI_FAMILY_APP135 # endif136 # if defined(WINAPI_FAMILY_PHONE_APP) && WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP137 # define Q_OS_WINRT138 # elif WINAPI_FAMILY==WINAPI_FAMILY_PC_APP139 # define Q_OS_WINRT140 # else141 # define Q_OS_WIN32142 # endif143 # else144 # define Q_OS_WIN32145 # endif146 #elif defined(__sun) || defined(sun)147 # define Q_OS_SOLARIS148 #elif defined(hpux) || defined(__hpux)149 # define Q_OS_HPUX150 #elif defined(__ultrix) || defined(ultrix)151 # define Q_OS_ULTRIX152 #elif defined(sinix)153 # define Q_OS_RELIANT154 #elif defined(__native_client__)155 # define Q_OS_NACL156 #elif defined(__linux__) || defined(__linux)157 # define Q_OS_LINUX158 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)159 # ifndef __FreeBSD_kernel__160 # define Q_OS_FREEBSD161 # endif162 # define Q_OS_FREEBSD_KERNEL163 # define Q_OS_BSD4164 #elif defined(__NetBSD__)165 # define Q_OS_NETBSD166 # define Q_OS_BSD4167 #elif defined(__OpenBSD__)168 # define Q_OS_OPENBSD169 # define Q_OS_BSD4170 #elif defined(__bsdi__)171 # define Q_OS_BSDI172 # define Q_OS_BSD4173 #elif defined(__INTERIX)174 # define Q_OS_INTERIX175 # define Q_OS_BSD4176 #elif defined(__sgi)177 # define Q_OS_IRIX178 #elif defined(__osf__)179 # define Q_OS_OSF180 #elif defined(_AIX)181 # define Q_OS_AIX182 #elif defined(__Lynx__)183 # define Q_OS_LYNX184 #elif defined(__GNU__)185 # define Q_OS_HURD186 #elif defined(__DGUX__)187 # define Q_OS_DGUX188 #elif defined(__QNXNTO__)189 # define Q_OS_QNX190 #elif defined(_SEQUENT_)191 # define Q_OS_DYNIX192 #elif defined(_SCO_DS) /* SCO OpenServer 5 + GCC */193 # define Q_OS_SCO194 #elif defined(__USLC__) /* all SCO platforms + UDK or OUDK */195 # define Q_OS_UNIXWARE196 #elif defined(__svr4__) && defined(i386) /* Open UNIX 8 + GCC */197 # define Q_OS_UNIXWARE198 #elif defined(__INTEGRITY)199 # define Q_OS_INTEGRITY200 #elif defined(VXWORKS) /* there is no "real" VxWorks define - this has to be set in the mkspec! */201 # define Q_OS_VXWORKS202 #elif defined(__HAIKU__)203 # define Q_OS_HAIKU204 #elif defined(__MAKEDEPEND__)205 #else206 # error "Qt has not been ported to this OS - see http://www.qt-project.org/"207 #endif208 209 #if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) || defined(Q_OS_WINRT)210 # define Q_OS_WIN211 #endif212 213 #if defined(Q_OS_WIN)214 # undef Q_OS_UNIX215 #elif !defined(Q_OS_UNIX)216 # define Q_OS_UNIX217 #endif218 219 // Compatibility synonyms220 #ifdef Q_OS_DARWIN221 #define Q_OS_MAC222 #endif223 #ifdef Q_OS_DARWIN32224 #define Q_OS_MAC32225 #endif226 #ifdef Q_OS_DARWIN64227 #define Q_OS_MAC64228 #endif229 #ifdef Q_OS_MACOS230 #define Q_OS_MACX231 #define Q_OS_OSX232 #endif233 234 #ifdef Q_OS_DARWIN235 # include 236 # include 237 #238 # ifdef Q_OS_MACOS239 # if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_6240 # undef __MAC_OS_X_VERSION_MIN_REQUIRED241 # define __MAC_OS_X_VERSION_MIN_REQUIRED __MAC_10_6242 # endif243 # if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6244 # undef MAC_OS_X_VERSION_MIN_REQUIRED245 # define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6246 # endif247 # endif248 #249 # // Numerical checks are preferred to named checks, but to be safe250 # // we define the missing version names in case Qt uses them.251 #252 # if !defined(__MAC_10_7)253 # define __MAC_10_7 1070254 # endif255 # if !defined(__MAC_10_8)256 # define __MAC_10_8 1080257 # endif258 # if !defined(__MAC_10_9)259 # define __MAC_10_9 1090260 # endif261 # if !defined(__MAC_10_10)262 # define __MAC_10_10 101000263 # endif264 # if !defined(__MAC_10_11)265 # define __MAC_10_11 101100266 # endif267 # if !defined(__MAC_10_12)268 # define __MAC_10_12 101200269 # endif270 # if !defined(__MAC_10_13)271 # define __MAC_10_13 101300272 # endif273 # if !defined(MAC_OS_X_VERSION_10_7)274 # define MAC_OS_X_VERSION_10_7 1070275 # endif276 # if !defined(MAC_OS_X_VERSION_10_8)277 # define MAC_OS_X_VERSION_10_8 1080278 # endif279 # if !defined(MAC_OS_X_VERSION_10_9)280 # define MAC_OS_X_VERSION_10_9 1090281 # endif282 # if !defined(MAC_OS_X_VERSION_10_10)283 # define MAC_OS_X_VERSION_10_10 101000284 # endif285 # if !defined(MAC_OS_X_VERSION_10_11)286 # define MAC_OS_X_VERSION_10_11 101100287 # endif288 # if !defined(MAC_OS_X_VERSION_10_12)289 # define MAC_OS_X_VERSION_10_12 101200290 # endif291 # if !defined(MAC_OS_X_VERSION_10_13)292 # define MAC_OS_X_VERSION_10_13 101300293 # endif294 #295 # if !defined(__IPHONE_4_3)296 # define __IPHONE_4_3 40300297 # endif298 # if !defined(__IPHONE_5_0)299 # define __IPHONE_5_0 50000300 # endif301 # if !defined(__IPHONE_5_1)302 # define __IPHONE_5_1 50100303 # endif304 # if !defined(__IPHONE_6_0)305 # define __IPHONE_6_0 60000306 # endif307 # if !defined(__IPHONE_6_1)308 # define __IPHONE_6_1 60100309 # endif310 # if !defined(__IPHONE_7_0)311 # define __IPHONE_7_0 70000312 # endif313 # if !defined(__IPHONE_7_1)314 # define __IPHONE_7_1 70100315 # endif316 # if !defined(__IPHONE_8_0)317 # define __IPHONE_8_0 80000318 # endif319 # if !defined(__IPHONE_8_1)320 # define __IPHONE_8_1 80100321 # endif322 # if !defined(__IPHONE_8_2)323 # define __IPHONE_8_2 80200324 # endif325 # if !defined(__IPHONE_8_3)326 # define __IPHONE_8_3 80300327 # endif328 # if !defined(__IPHONE_8_4)329 # define __IPHONE_8_4 80400330 # endif331 # if !defined(__IPHONE_9_0)332 # define __IPHONE_9_0 90000333 # endif334 # if !defined(__IPHONE_9_1)335 # define __IPHONE_9_1 90100336 # endif337 # if !defined(__IPHONE_9_2)338 # define __IPHONE_9_2 90200339 # endif340 # if !defined(__IPHONE_9_3)341 # define __IPHONE_9_3 90300342 # endif343 # if !defined(__IPHONE_10_0)344 # define __IPHONE_10_0 100000345 # endif346 # if !defined(__IPHONE_10_1)347 # define __IPHONE_10_1 100100348 # endif349 # if !defined(__IPHONE_10_2)350 # define __IPHONE_10_2 100200351 # endif352 # if !defined(__IPHONE_10_3)353 # define __IPHONE_10_3 100300354 # endif355 # if !defined(__IPHONE_11_0)356 # define __IPHONE_11_0 110000357 # endif358 #endif359 360 #ifdef __LSB_VERSION__361 # if __LSB_VERSION__ < 40362 # error "This version of the Linux Standard Base is unsupported"363 # endif364 #ifndef QT_LINUXBASE365 # define QT_LINUXBASE366 #endif367 #endif368 369 #endif // QSYSTEMDETECTION_H
各种操作系统的宏定义。
【2】不同编译器不同版本的宏
(1)不同编译器
1.1 Visual C++ 编译器预定义宏:
1.2 GCC 编译器预定义宏:
(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 #elif11 #pragma comment( lib, "SomeLib-vc71-r.lib" )12 #endif13 #elif _MSC_VER >=1200 // for vc614 #ifdef _DEBUG15 #pragma comment( lib, "SomeLib-vc6-d.lib" )16 #elif17 #pragma comment( lib, "SomeLib-vc6-r.lib" )18 #endif19 #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\n");7 //...8 #endif
Good Good Study, Day Day Up.
顺序 选择 循环 总结