Cross Platform Note: STD C++ Preprocessor directives & pre-defined macros

來源:互聯網
上載者:User

標籤:

ref: http://www.cplusplus.com/doc/tutorial/preprocessor/

concolusion:

 

directives:

#define #undef

#include

#if #else #elif #endif #ifdef #ifndef

#error ( #warning is NOT A STANDARD DIRECTIVE,  since not shown in the page above, but most compilers have it. more: http://stackoverflow.com/questions/171435/portability-of-warning-preprocessor-directive )

#line

#pragma ( all compiler support #pragma to set options, but "These options are specific for the platform and the compiler you use.", so no standard options.)

 

macros:

__LINE__

__FILE__

__DATE__

__TIME__

(__FUNCTION__ is NOT STANDARD MACRO. __FUNCTION__ is defined on both MSVC & GCC; GCC better use __PRETTY_FUNCTION__. c99 has standard __func__)

__cplusplus (has a value to identifying C++98 / 11 )

__STD_HOSTED__

optional: __STDC__ __STDC_VERSION__ __STDC_MB_MIGHT_NEQ_WC__ __STDC_ISO_10646__ __STDCPP_STRICT_POINTER_SAFETY__ __STDCPP_THREADS__

 

Example of use non standard macros (compiler specific) in Blade:

  1 /********************************************************************  2     created:    2009/02/07  3     filename:     Platform.h  4     author:        Crazii  5       6     purpose:    the platform dependent definitions  7 *********************************************************************/  8 #ifndef __Blade_Platform_h__  9 #define __Blade_Platform_h__ 10  11  12 /************************************************************************/ 13 /* processor unit                                                                     */ 14 /************************************************************************/ 15 #define BLADE_PROCESSOR_X86        (0) 16 #define BLADE_PROCESSOR_IA64    (1) 17 #define BLADE_PROCESSOR_PPC        (2) 18 #define BLADE_PROCESSOR_ARM        (3) 19  20 #if defined ARM || defined __arm__ || defined _ARM 21 #    define BLADE_PROCESSOR BLADE_PROCESSOR_ARM 22  23 #elif defined __ia64 || defined _M_IA64 || defined __ia64__  24 #    define BLADE_PROCESSOR BLADE_PROCESSOR_IA64 25  26 #elif defined __X86__ || defined __i386__ || defined i386 || defined _M_IX86 || defined __386__ || defined __x86_64__ || defined _M_X64 27 #    define BLADE_PROCESSOR BLADE_PROCESSOR_X86 28  29 #elif defined __PPC__ || defined __POWERPC__  || defined powerpc || defined _POWER || defined __ppc__ || defined __powerpc__ || defined _M_PPC 30 #  define BLADE_PROCESSOR BLADE_PROCESSOR_PPC 31  32 #else 33 #    error "processor not supported yet." 34 #endif 35  36 /************************************************************************/ 37 /* this is the system specific,windows and linux, etc                                                                 */ 38 /************************************************************************/ 39  40 #define BLADE_PLATFORM_WINDOWS    (0) 41 #define BLADE_PLATFORM_WINPHONE    (1) 42  43 #define BLADE_PLATFORM_LINUX    (2) 44 #define BLADE_PLATFORM_ANDROID    (3) 45 #define BLADE_PLATFORM_IOS        (4) 46 #define BLADE_PLATFORM_MAC        (5) 47  48 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) 49  50 #    define BLADE_PLATFORM BLADE_PLATFORM_WINDOWS 51  52 #elif defined(WP8) && defined(_WP8) 53  54 #    define BLADE_PLATFORM BLADE_PLATFORM_WINPHONE 55  56 #elif defined(ANDROID) || defined(__ANDROID__)//since android will also define __linux__, we need check android first 57  58 #define BLADE_PLATFORM BLADE_PLATFORM_ANDROID 59  60 #elif defined(__linux__) 61  62 #    define BLADE_PLATFORM BLADE_PLATFORM_LINUX 63  64 #else 65 #    error "current system not support yet." 66 #endif 67  68 //Careful on using those class system definition. different system may have different APIs. 69 //i.e. Unix POSIX version difference, windows Desktop/Mobile API differences. 70  71 #if defined(__unix__) 72 #    define BLADE_IS_UNIX_CLASS_SYSTEM 1 73 #else 74 #    define BLADE_IS_UNIX_CLASS_SYSTEM 0 75 #endif 76  77 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_WIN32_WCE) || defined(WINRT) || defined(_WINRT) || defined(WP8) || defined(_WP8) 78 #    define BLADE_IS_WINDOWS_CLASS_SYSTEM 1 79 #else 80 #    define BLADE_IS_WINDOWS_CLASS_SYSTEM 0 81 #endif 82  83 /************************************************************************/ 84 /* Architecture (32/64 bit)                                                                     */ 85 /************************************************************************/ 86  87 #define BLADE_ARCHITECTURE_32BIT (0) 88 #define BLADE_ARCHITECTURE_64BIT (1) 89  90 #if defined(__x86_64__) || defined(_M_X64) || defined(_M_IA64) || defined(_M_AMD64) || defined(__powerpc64__) || defined(__alpha__) || defined(__ia64__) || defined(__s390__) || defined(__s390x__) 91 #   define BLADE_ARCHITECTURE BLADE_ARCHITECTURE_64BIT 92 #    define BLADE_MEMORY_ALIGNMENT (16) 93 #else 94 #   define BLADE_ARCHITECTURE BLADE_ARCHITECTURE_32BIT 95 #    define BLADE_MEMORY_ALIGNMENT (8) 96 #endif 97  98 /************************************************************************/ 99 /* this is the compiler specific ,MSVC and GCC currently supported */100 /************************************************************************/101 #define BLADE_COMPILER_MSVC (0)102 #define BLADE_COMPILER_GNUC (1)103 104 105 #if defined(_MSC_VER)106 #    define BLADE_COMPILER BLADE_COMPILER_MSVC107 #    define BLADE_ALIGNED(n) __declspec(align(n))108 #    define BLADE_CDECL    __cdecl109 #    define BLADE_STDCALL  __stdcall110 #    define BLADE_FASTCALL __fastcall111 #    define BLADE_FUNCTION    __FUNCTION__112 #    define BLADE_ALLWAYS_INLINE __forceinline113 #elif defined(__GNUC__)114 #    define BLADE_COMPILER BLADE_COMPILER_GNUC115 #    define BLADE_ALIGNED(n)   __attribute__((aligned(n)))116 #    define BLADE_CDECL __attribute__((cdecl))117 #    define BLADE_STDCALL __attribute__((stdcall))118 #    define BLADE_FASTCALL __attribute__((fastcall))119 #    define BLADE_FUNCTION    __PRETTY_FUNCTION__120 #    define BLADE_ALLWAYS_INLINE __attribute__((always_inline))121 #else122 #    error "Compiler not supported yet."123 #    define BLADE_ALLWAYS_INLINE inline124 #endif125 126 #if defined(_DEBUG) || defined(DEBUG)127 #    define BLADE_DEBUG 1128 #else129 #    define BLADE_DEBUG 0130 #endif131 132 #define BLADE_ENDIAN_LITTLE        0133 #define BLADE_ENDIAN_BIG        1134 135 //endian macros136 //HOWTO: ARM processor can have dynamic endian with endian map, how to check?137 138 #if BLADE_PROCESSOR == BLADE_PROCESSOR_X86 || 139     BLADE_PROCESSOR == BLADE_PROCESSOR_ARM140 141 #define BLADE_ENDIAN BLADE_ENDIAN_LITTLE142 143 #else144 145 #define BLADE_ENDIAN BLADE_ENDIAN_BIG146 147 #endif148 149 //make unicode macro having a value150 #if defined(UNICODE) || defined(_UNICODE)151 #    undef UNICODE152 #    define UNICODE 1153 #    define BLADE_UNICODE 1154 #else155 #    define BLADE_UNICODE 0156 #endif157 158 159 #endif // __Blade_Platform_h__

 

Cross Platform Note: STD C++ Preprocessor directives & pre-defined macros

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.