Cross Platform Note: STD C++ Preprocessor directives & pre-defined macros
2015-08-09 21:57
561 查看
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:
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:
/******************************************************************** created: 2009/02/07 filename: Platform.h author: Crazii purpose: the platform dependent definitions *********************************************************************/ #ifndef __Blade_Platform_h__ #define __Blade_Platform_h__ /************************************************************************/ /* processor unit */ /************************************************************************/ #define BLADE_PROCESSOR_X86 (0) #define BLADE_PROCESSOR_IA64 (1) #define BLADE_PROCESSOR_PPC (2) #define BLADE_PROCESSOR_ARM (3) #if defined ARM || defined __arm__ || defined _ARM # define BLADE_PROCESSOR BLADE_PROCESSOR_ARM #elif defined __ia64 || defined _M_IA64 || defined __ia64__ # define BLADE_PROCESSOR BLADE_PROCESSOR_IA64 #elif defined __X86__ || defined __i386__ || defined i386 || defined _M_IX86 || defined __386__ || defined __x86_64__ || defined _M_X64 # define BLADE_PROCESSOR BLADE_PROCESSOR_X86 #elif defined __PPC__ || defined __POWERPC__ || defined powerpc || defined _POWER || defined __ppc__ || defined __powerpc__ || defined _M_PPC # define BLADE_PROCESSOR BLADE_PROCESSOR_PPC #else # error "processor not supported yet." #endif /************************************************************************/ /* this is the system specific,windows and linux, etc */ /************************************************************************/ #define BLADE_PLATFORM_WINDOWS (0) #define BLADE_PLATFORM_WINPHONE (1) #define BLADE_PLATFORM_LINUX (2) #define BLADE_PLATFORM_ANDROID (3) #define BLADE_PLATFORM_IOS (4) #define BLADE_PLATFORM_MAC (5) #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) # define BLADE_PLATFORM BLADE_PLATFORM_WINDOWS #elif defined(WP8) && defined(_WP8) # define BLADE_PLATFORM BLADE_PLATFORM_WINPHONE #elif defined(ANDROID) || defined(__ANDROID__)//since android will also define __linux__, we need check android first #define BLADE_PLATFORM BLADE_PLATFORM_ANDROID #elif defined(__linux__) # define BLADE_PLATFORM BLADE_PLATFORM_LINUX #else # error "current system not support yet." #endif //Careful on using those class system definition. different system may have different APIs. //i.e. Unix POSIX version difference, windows Desktop/Mobile API differences. #if defined(__unix__) # define BLADE_IS_UNIX_CLASS_SYSTEM 1 #else # define BLADE_IS_UNIX_CLASS_SYSTEM 0 #endif #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_WIN32_WCE) || defined(WINRT) || defined(_WINRT) || defined(WP8) || defined(_WP8) # define BLADE_IS_WINDOWS_CLASS_SYSTEM 1 #else # define BLADE_IS_WINDOWS_CLASS_SYSTEM 0 #endif /************************************************************************/ /* Architecture (32/64 bit) */ /************************************************************************/ #define BLADE_ARCHITECTURE_32BIT (0) #define BLADE_ARCHITECTURE_64BIT (1) #if defined(__x86_64__) || defined(_M_X64) || defined(_M_IA64) || defined(_M_AMD64) || defined(__powerpc64__) || defined(__alpha__) || defined(__ia64__) || defined(__s390__) || defined(__s390x__) # define BLADE_ARCHITECTURE BLADE_ARCHITECTURE_64BIT # define BLADE_MEMORY_ALIGNMENT (16) #else # define BLADE_ARCHITECTURE BLADE_ARCHITECTURE_32BIT # define BLADE_MEMORY_ALIGNMENT (8) #endif /************************************************************************/ /* this is the compiler specific ,MSVC and GCC currently supported */ /************************************************************************/ #define BLADE_COMPILER_MSVC (0) #define BLADE_COMPILER_GNUC (1) #if defined(_MSC_VER) # define BLADE_COMPILER BLADE_COMPILER_MSVC # define BLADE_ALIGNED(n) __declspec(align(n)) # define BLADE_CDECL __cdecl # define BLADE_STDCALL __stdcall # define BLADE_FASTCALL __fastcall # define BLADE_FUNCTION __FUNCTION__ # define BLADE_ALLWAYS_INLINE __forceinline #elif defined(__GNUC__) # define BLADE_COMPILER BLADE_COMPILER_GNUC # define BLADE_ALIGNED(n) __attribute__((aligned(n))) # define BLADE_CDECL __attribute__((cdecl)) # define BLADE_STDCALL __attribute__((stdcall)) # define BLADE_FASTCALL __attribute__((fastcall)) # define BLADE_FUNCTION __PRETTY_FUNCTION__ # define BLADE_ALLWAYS_INLINE __attribute__((always_inline)) #else # error "Compiler not supported yet." # define BLADE_ALLWAYS_INLINE inline #endif #if defined(_DEBUG) || defined(DEBUG) # define BLADE_DEBUG 1 #else # define BLADE_DEBUG 0 #endif #define BLADE_ENDIAN_LITTLE 0 #define BLADE_ENDIAN_BIG 1 //endian macros //HOWTO: ARM processor can have dynamic endian with endian map, how to check? #if BLADE_PROCESSOR == BLADE_PROCESSOR_X86 || \ BLADE_PROCESSOR == BLADE_PROCESSOR_ARM #define BLADE_ENDIAN BLADE_ENDIAN_LITTLE #else #define BLADE_ENDIAN BLADE_ENDIAN_BIG #endif //make unicode macro having a value #if defined(UNICODE) || defined(_UNICODE) # undef UNICODE # define UNICODE 1 # define BLADE_UNICODE 1 #else # define BLADE_UNICODE 0 #endif #endif // __Blade_Platform_h__
相关文章推荐
- 【C/C++】浅谈C/C++之深浅拷贝
- [leetcode-89]Gray Code(c++)
- c语言复习 —— 数组
- C++ List 容器
- 23种设计模式--单例模式(c++版和php版)
- C++流之标准I/O流
- 【C/C++学院】0726-cppIDE/一级指针/指针数组/函数指针/函数指针数组/二级指针
- c++ shared_ptr 使用注意事项. 2
- [C++11 并发编程] 05 - Mutex 基本操作
- QQ连连看辅助c++版本的
- (C/C++学习笔记)多继承的二义性
- C++对象模型——Virtual Member Functions (虚拟成员函数)(第四章)
- c语言中的函数
- c语言中 指针和一维数组 简要总结
- 【整理】C语言中常见的字符串操作函数
- C语言—全局变量
- c++ shared_ptr 使用注意事项. 1
- 静态字符串模式匹配模板库
- C++:复制构造函数___浅拷贝
- C++模板详解(二)