游戏人生

不积跬步,无以至千里;不积小流,无以成江海。

导航

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

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_MSVC
107 #    define BLADE_ALIGNED(n) __declspec(align(n))
108 #    define BLADE_CDECL    __cdecl
109 #    define BLADE_STDCALL  __stdcall
110 #    define BLADE_FASTCALL __fastcall
111 #    define BLADE_FUNCTION    __FUNCTION__
112 #    define BLADE_ALLWAYS_INLINE __forceinline
113 #elif defined(__GNUC__)
114 #    define BLADE_COMPILER BLADE_COMPILER_GNUC
115 #    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 #else
122 #    error "Compiler not supported yet."
123 #    define BLADE_ALLWAYS_INLINE inline
124 #endif
125 
126 #if defined(_DEBUG) || defined(DEBUG)
127 #    define BLADE_DEBUG 1
128 #else
129 #    define BLADE_DEBUG 0
130 #endif
131 
132 #define BLADE_ENDIAN_LITTLE        0
133 #define BLADE_ENDIAN_BIG        1
134 
135 //endian macros
136 //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_ARM
140 
141 #define BLADE_ENDIAN BLADE_ENDIAN_LITTLE
142 
143 #else
144 
145 #define BLADE_ENDIAN BLADE_ENDIAN_BIG
146 
147 #endif
148 
149 //make unicode macro having a value
150 #if defined(UNICODE) || defined(_UNICODE)
151 #    undef UNICODE
152 #    define UNICODE 1
153 #    define BLADE_UNICODE 1
154 #else
155 #    define BLADE_UNICODE 0
156 #endif
157 
158 
159 #endif // __Blade_Platform_h__

 

posted on 2015-08-09 21:57  crazii  阅读(418)  评论(0编辑  收藏  举报