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系统是我们最常用的系统。

说明
_WIN1616位环境
_WIN3232和64位环境
_WIN6464位环境

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.0800
3.0900
4.01000
4.21020
5.01100
6.01200
6.0 SP61200
7.01300
7.1 (2003)1310
8.0 (2005)1400
9.0 (2008)1500
9.0 SP11500
10.0 (2010)1600
10.0 (2010) SP11600
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_CC编译器
__SUNPRO_CCC++编译器

4.主流架构

4.1 Intel x86

说明
i386 __i386 __i386__由GNU C 定义
__i386Sun 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 定义
__386Diab 定义

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

相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页