C/C++跨平臺程序基礎知識

C/C++跨平臺程序基礎知識


如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033

目錄


前言

很多時候,我們寫的程序可能要在多個操作系統運行,這個時候就要求我們的程序儘可能的可以在不改動的情況下完成這個功能。這就是本文要講的內容。


1.主流操作系統類別

1.1 UNIX System V

▪ A/UX ▪ AIX ▪ HP-UX ▪ IRIX ▪ LynxOS ▪ SCO OpenServer ▪ Tru64 ▪ Xenix ▪ Solaris ▪ OS/2

1.2 BSD UNIX-386BSD

▪ BSD/OS ▪ FreeBSD ▪ NetBSD ▪ NEXTSTEP ▪ Mac OS X ▪ iOS ▪ OpenBSD ▪ SUN OS▪ OpenSolaris

1.3 UNIX-Like

▪ GNU ▪ Linux ▪ Android ▪ Debian ▪ Ubuntu ▪ Red Hat ▪ Linux Mint ▪ Minix ▪ QNX ▪ GNU/Linux ▪ GNU/Hurd ▪ Debian GNU/Hurd ▪ GNU/kFreeBSD ▪ StartOS

1.4 Windows

Microsoft Windows是美國微軟公司研發的一套操作系統

1.4.1 For DOS

▪ Windows 1.0 ( 1985) ▪ Windows 2.0 ( 1987) ▪ Windows 2.1 ( 1988)
▪ windows 3.0 ( 1990) ▪ windows 3.1 ( 1992) ▪ Windows 3.2 ( 1994)

1.4.2 Win 9x

▪ Windows 95 ( 1995) ▪ Windows97 ( 1996) ▪ Windows 98 ( 1998)
▪ Windows 98 SE ( 1999) ▪ Windows Me ( 2000)

1.4.3 NT系列

1.4.3.1 早期版本

▪ Windows NT 3.1 ( 1993) ▪ Windows NT 3.5 ( 1994) ▪ Windows NT 3.51 ( 1995)
▪ Windows NT 4.0 ( 1996) ▪ Windows 2000 ( 2000)

1.4.3.2 客戶端

▪ windows xp ( 2001) ▪ Windows Vista ( 2005) ▪ Windows 7 ( 2009)
▪ Windows Thin PC ( 2011) ▪ Windows 8 ( 2012) ▪ Windows RT ( 2012)
▪ Windows 8.1 ( 2013)

1.4.3.3 服務器

▪ Windows Server 2003 ( 2003) ▪ Windows Server 2008 ( 2008)
▪ Windows Home Server ( 2008) ▪ Windows HPC Server 2008 ( 2010)
▪ Windows Small Business Server ( 2011) ▪ Windows Essential Business Server
▪ Windows Server 2012 ( 2012) ▪ Windows Server 2012 R2 ( 2013)

1.4.3.4 特別版本

▪ Windows PE ▪ Windows Azure
▪ Windows Fundamentals for Legacy PCs

1.4.4嵌入式系統

▪ Windows CE ▪ Windows Mobile ( 2000) ▪ Windows Phone ( 2010)

1.5 DOS

dos,是磁盤操作系統的縮寫,是個人計算機上的一類操作系統。


2.主流操作系統簡介

2.1 Windows

Microsoft Windows系統是我們最常用的系統。

說明
_WIN16 16位環境
_WIN32 32和64位環境
_WIN64 64位環境

2.2 Linux kernel(Linux內核)

基於Linux內核的系統定義了這些宏。 有兩種主要的基於Linux的操作系統:GNU/Linux和Android

說明
__linux__
linux 已過時(不符合POSIX標準)
__linux 已過時(不符合POSIX標準)

2.2.1 GNU/Linux

說明
__gnu_linux__

2.2.2 Android

Android是一種基於Linux的自由及開放源代碼的操作系統,主要使用於移動設備,如智能手機和平板電腦,由Google公司和開放手機聯盟領導及開發。

說明
__ANDROID__

注意:Android系統基於Linux系統,並且Linux宏也是爲Android定義的

Notice that Android is based on Linux, and that the Linux macros also are defined for Android.

2.3 APPLE系列

Darwin System(達爾文操作系統) 是一個由蘋果公司(Apple Inc.)開發的 UNIX 操作系統。自2000年後,Darwin 是蘋果所有操作系統的基礎,包括 macOS(原名 Mac OS X ,後縮寫爲 OS X,至 WWDC 2016 改名爲 macOS)、iOS、watchOS 和 tvOS。

說明
__APPLE__ && __MACH__ Mac OS X 由GNU C和Intel C ++定義

2.4 FreeBSD

說明
__FreeBSD__
__FreeBSD_kernel__ 從FreeBSD 8.3, 9.1, 10.0.1開始

2.5 Solaris

說明
sun
__sun

3.主流編譯器

3.1 Microsoft Visual C++

MSVC是微軟Windows平臺Visual Studio自帶的C/C++編譯器。

說明
_MSC_VER
Visual C++ _MSC_VER
1.0 800
3.0 900
4.0 1000
4.2 1020
5.0 1100
6.0 1200
6.0 SP6 1200
7.0 1300
7.1 (2003) 1310
8.0 (2005) 1400
9.0 (2008) 1500
9.0 SP1 1500
10.0 (2010) 1600
10.0 (2010) SP1 1600
11.0 (2012) 1700
12.0 (2013) 1800
14.0 (2015) 1900
14.0 (2015 Update 1) 1900
14.0 (2015 Update 2) 1900
14.0 (2015 Update 3) 1900
15.0 (2017) 1910

3.2 GCC C/C++

GCC原名GNU C Compiler,後來逐漸支持更多的語言編譯(C++、Fortran、Pascal、Objective-C、Java、Ada、Go等),所以變成了GNU Compiler Collection(GNU編譯器套裝),是一套由GNU工程開發的支持多種編程語言的編譯器。GCC是自由軟件發展過程中的著名例子,由自由軟件基金會以GPL協議發佈,是大多數類Unix(如Linux、BSD、Mac OS X等)的標準編譯器,而且適用於Windows(藉助其他移植項目實現的,比如MingW、Cygwin等)。GCC支持多種計算機體系芯片,如x86、ARM,並已移植到其他多種硬件平臺。

說明
__GNUC__
__GNUC__
__GNUC_MINOR__
__GNUC_PATCHLEVEL__

These macros are 由all GNU compilers that use the C preprocessor: C, C++, Objective-C 
and Fortran. Their values are the major version, minor version, and patch level of the compiler,
 as integer constants. For example, GCC 3.2.1 will define __GNUC__ to 3, __GNUC_MINOR__ to 2, and
 __GNUC_PATCHLEVEL__ to 1. These macros are also defined if you invoke the preprocessor directly.

這些宏由所有使用C預處理器的GNU編譯器定義:C,C ++,Objective-C和Fortran。 
它們的值是編譯器的主版本,次版本和補丁級別,作爲整數常量。 例如,GCC 3.2.1將把__GNUC__定義爲3,
__GNUC_MINOR__定義爲2,__GNUC_PATCHLEVEL__定義爲1.如果直接調用預處理器,也定義了這些宏。

3.3 Cygwin

Cygwin是一個Windows下Unix-like模擬環境,具體說就是Unix-like接口(OS API,命令行)重定向層,其目的是不修改軟件源碼僅重新編譯就可以將Unix-like系統上的軟件移植到Windows上(這個移植也許還算不上嚴格意義上的無縫移植)。始於1995年,最初作爲Cygnus軟件公司工程師Steve Chamberlain的一個項目。

說明
__CYGWIN__

3.4 MinGW 和 MinGW-w64

MingW(Minimalist GNU on Windows)是一個Linux/Windows下的可以把軟件源碼中Unix-like OS API調用通過頭文件翻譯替換成相應的Windows API調用的編譯環境,其目的和Cygwin相同。從而把Linux上的軟件在不修改源碼的情況下編譯爲可直接在Win下執行的exe。

說明
__MINGW32__ MinGW32、 MinGW-w64 32位、MinGW-w64 64位
__MINGW64__ MinGW-w64 64位

3.5 Oracle Solaris Studio

說明
__SUNPRO_C C編譯器
__SUNPRO_CC C++編譯器

4.主流架構

4.1 Intel x86

說明
i386 __i386 __i386__ 由GNU C 定義
__i386 Sun Studio定義
__i386 __IA32__ Stratus VOS C 定義
_M_I86 僅針對16位體系結構定義由Visual Studio,Digital Mars和Watcom C / C ++定義
_M_IX86 僅針對32位體系結構定義,由Visual Studio,Intel C / C ++,Digital Mars和Watcom C / C ++定義
__X86__ Watcom C/C++ 定義
_X86_ MinGW32 定義
__THW_INTEL__ XL C/C++ 定義
__I86__ Digital Mars 定義
__INTEL__ CodeWarrior 定義
__386 Diab 定義

4.2 Intel Itanium (IA-64)

說明
__ia64__ __IA64 __IA64__ 由GNU C定義
__ia64 由HP aCC 定義
_M_IA64 由Visual Studio 定義
_M_IA64 由Intel C/C++ 定義
__itanium__ 由Intel C/C++ 定義

4.3 ARM

說明
__arm__ 由GNU C 和 RealView 定義
__thumb__ 由GNU C 和RealView在Thumb模式下定義
__TARGET_ARCH_ARM __TARGET_ARCH_THUMB 由RealView 定義
_ARM 由ImageCraft C 定義
_M_ARM 由Visual Studio 定義
_M_ARMT 由Visual Studio in Thumb mode 定義

4.4 ARM64

說明
__aarch64__ 由GNU C定義

5.QT跨平臺頭文件

QT5.11
qsystemdetection.h

#ifndef QGLOBAL_H
# include <QtCore/qglobal.h>
#endif

#ifndef QSYSTEMDETECTION_H
#define QSYSTEMDETECTION_H

/*
   The operating system, must be one of: (Q_OS_x)

     DARWIN   - Any Darwin system (macOS, iOS, watchOS, tvOS)
     MACOS    - macOS
     IOS      - iOS
     WATCHOS  - watchOS
     TVOS     - tvOS
     WIN32    - Win32 (Windows 2000/XP/Vista/7 and Windows Server 2003/2008)
     WINRT    - WinRT (Windows Runtime)
     CYGWIN   - Cygwin
     SOLARIS  - Sun Solaris
     HPUX     - HP-UX
     LINUX    - Linux [has variants]
     FREEBSD  - FreeBSD [has variants]
     NETBSD   - NetBSD
     OPENBSD  - OpenBSD
     INTERIX  - Interix
     AIX      - AIX
     HURD     - GNU Hurd
     QNX      - QNX [has variants]
     QNX6     - QNX RTP 6.1
     LYNX     - LynxOS
     BSD4     - Any BSD 4.4 system
     UNIX     - Any UNIX BSD/SYSV system
     ANDROID  - Android platform
     HAIKU    - Haiku

   The following operating systems have variants:
     LINUX    - both Q_OS_LINUX and Q_OS_ANDROID are defined when building for Android
              - only Q_OS_LINUX is defined if building for other Linux systems
     FREEBSD  - Q_OS_FREEBSD is defined only when building for FreeBSD with a BSD userland
              - Q_OS_FREEBSD_KERNEL is always defined on FreeBSD, even if the userland is from GNU
*/

#if defined(__APPLE__) && (defined(__GNUC__) || defined(__xlC__) || defined(__xlc__))
#  include <TargetConditionals.h>
#  if defined(TARGET_OS_MAC) && TARGET_OS_MAC
#    define Q_OS_DARWIN
#    define Q_OS_BSD4
#    ifdef __LP64__
#      define Q_OS_DARWIN64
#    else
#      define Q_OS_DARWIN32
#    endif
#    if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
#      define QT_PLATFORM_UIKIT
#      if defined(TARGET_OS_WATCH) && TARGET_OS_WATCH
#        define Q_OS_WATCHOS
#      elif defined(TARGET_OS_TV) && TARGET_OS_TV
#        define Q_OS_TVOS
#      else
#        // TARGET_OS_IOS is only available in newer SDKs,
#        // so assume any other iOS-based platform is iOS for now
#        define Q_OS_IOS
#      endif
#    else
#      // TARGET_OS_OSX is only available in newer SDKs,
#      // so assume any non iOS-based platform is macOS for now
#      define Q_OS_MACOS
#    endif
#  else
#    error "Qt has not been ported to this Apple platform - see http://www.qt.io/developers"
#  endif
#elif defined(__ANDROID__) || defined(ANDROID)
#  define Q_OS_ANDROID
#  define Q_OS_LINUX
#elif defined(__CYGWIN__)
#  define Q_OS_CYGWIN
#elif !defined(SAG_COM) && (!defined(WINAPI_FAMILY) || WINAPI_FAMILY==WINAPI_FAMILY_DESKTOP_APP) && (defined(WIN64) || defined(_WIN64) || defined(__WIN64__))
#  define Q_OS_WIN32
#  define Q_OS_WIN64
#elif !defined(SAG_COM) && (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
#  if defined(WINAPI_FAMILY)
#    ifndef WINAPI_FAMILY_PC_APP
#      define WINAPI_FAMILY_PC_APP WINAPI_FAMILY_APP
#    endif
#    if defined(WINAPI_FAMILY_PHONE_APP) && WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP
#      define Q_OS_WINRT
#    elif WINAPI_FAMILY==WINAPI_FAMILY_PC_APP
#      define Q_OS_WINRT
#    else
#      define Q_OS_WIN32
#    endif
#  else
#    define Q_OS_WIN32
#  endif
#elif defined(__sun) || defined(sun)
#  define Q_OS_SOLARIS
#elif defined(hpux) || defined(__hpux)
#  define Q_OS_HPUX
#elif defined(__native_client__)
#  define Q_OS_NACL
#elif defined(__linux__) || defined(__linux)
#  define Q_OS_LINUX
#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
#  ifndef __FreeBSD_kernel__
#    define Q_OS_FREEBSD
#  endif
#  define Q_OS_FREEBSD_KERNEL
#  define Q_OS_BSD4
#elif defined(__NetBSD__)
#  define Q_OS_NETBSD
#  define Q_OS_BSD4
#elif defined(__OpenBSD__)
#  define Q_OS_OPENBSD
#  define Q_OS_BSD4
#elif defined(__INTERIX)
#  define Q_OS_INTERIX
#  define Q_OS_BSD4
#elif defined(_AIX)
#  define Q_OS_AIX
#elif defined(__Lynx__)
#  define Q_OS_LYNX
#elif defined(__GNU__)
#  define Q_OS_HURD
#elif defined(__QNXNTO__)
#  define Q_OS_QNX
#elif defined(__INTEGRITY)
#  define Q_OS_INTEGRITY
#elif defined(VXWORKS) /* there is no "real" VxWorks define - this has to be set in the mkspec! */
#  define Q_OS_VXWORKS
#elif defined(__HAIKU__)
#  define Q_OS_HAIKU
#elif defined(__MAKEDEPEND__)
#else
#  error "Qt has not been ported to this OS - see http://www.qt-project.org/"
#endif

#if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) || defined(Q_OS_WINRT)
#  define Q_OS_WIN
#endif

#if defined(Q_OS_WIN)
#  undef Q_OS_UNIX
#elif !defined(Q_OS_UNIX)
#  define Q_OS_UNIX
#endif

// Compatibility synonyms
#ifdef Q_OS_DARWIN
#define Q_OS_MAC
#endif
#ifdef Q_OS_DARWIN32
#define Q_OS_MAC32
#endif
#ifdef Q_OS_DARWIN64
#define Q_OS_MAC64
#endif
#ifdef Q_OS_MACOS
#define Q_OS_MACX
#define Q_OS_OSX
#endif

#ifdef Q_OS_DARWIN
#  include <Availability.h>
#  include <AvailabilityMacros.h>
#
#  ifdef Q_OS_MACOS
#    if !defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_6
#       undef __MAC_OS_X_VERSION_MIN_REQUIRED
#       define __MAC_OS_X_VERSION_MIN_REQUIRED __MAC_10_6
#    endif
#    if !defined(MAC_OS_X_VERSION_MIN_REQUIRED) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6
#       undef MAC_OS_X_VERSION_MIN_REQUIRED
#       define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
#    endif
#  endif
#
#  // Numerical checks are preferred to named checks, but to be safe
#  // we define the missing version names in case Qt uses them.
#
#  if !defined(__MAC_10_11)
#       define __MAC_10_11 101100
#  endif
#  if !defined(__MAC_10_12)
#       define __MAC_10_12 101200
#  endif
#  if !defined(__MAC_10_13)
#       define __MAC_10_13 101300
#  endif
#  if !defined(__MAC_10_14)
#       define __MAC_10_14 101400
#  endif
#  if !defined(MAC_OS_X_VERSION_10_11)
#       define MAC_OS_X_VERSION_10_11 101100
#  endif
#  if !defined(MAC_OS_X_VERSION_10_12)
#       define MAC_OS_X_VERSION_10_12 101200
#  endif
#  if !defined(MAC_OS_X_VERSION_10_13)
#       define MAC_OS_X_VERSION_10_13 101300
#  endif
#  if !defined(MAC_OS_X_VERSION_10_14)
#       define MAC_OS_X_VERSION_10_14 101400
#  endif
#
#  if !defined(__IPHONE_10_0)
#       define __IPHONE_10_0 100000
#  endif
#  if !defined(__IPHONE_10_1)
#       define __IPHONE_10_1 100100
#  endif
#  if !defined(__IPHONE_10_2)
#       define __IPHONE_10_2 100200
#  endif
#  if !defined(__IPHONE_10_3)
#       define __IPHONE_10_3 100300
#  endif
#  if !defined(__IPHONE_11_0)
#       define __IPHONE_11_0 110000
#  endif
#  if !defined(__IPHONE_12_0)
#       define __IPHONE_12_0 120000
#  endif
#endif

#ifdef __LSB_VERSION__
#  if __LSB_VERSION__ < 40
#    error "This version of the Linux Standard Base is unsupported"
#  endif
#ifndef QT_LINUXBASE
#  define QT_LINUXBASE
#endif
#endif

#endif // QSYSTEMDETECTION_H

Refrence:

https://baike.baidu.com/item/linux/27050?fr=aladdin
https://sourceforge.net/p/predef/wiki/Home/
https://sourceforge.net/p/predef/wiki/OperatingSystems/

覺得文章對你有幫助,可以用微信掃描二維碼捐贈給博主,謝謝!
微信
如需轉載請標明出處:http://blog.csdn.net/itas109
QQ技術交流羣:129518033

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章