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__
时间: 2024-10-05 05:21:26

Cross Platform Note: STD C++ Preprocessor directives & pre-defined macros的相关文章

Transport Tablespaces with Reduced Downtime using Cross Platform Incremental Backup

OVERVIEW 1 目的 传统的传输表空间方式要求数据第一次由远端到目标端传输时,表空间必须置于read only模式,从而生产不可用.而XTTS方式则只需要在最后一次增量备份时将表空间置于read only模式,显著的减少了停机的时间 XTTS can significantly reduce the amount of downtime required to move data between platforms using enhanced RMAN's bility 2 oracle

Cross platform GUI for creating SSL certs with OpenSSL

Someone said: from : https://micksmix.wordpress.com/2012/08/09/xca-cross-platform-gui-for-creating-ssl-certs-with-openssl/ Download: http://sourceforge.net/projects/xca/ There are some of you that know your way around OpenSSL’s options in your sleep,

Qt编程之Qt样例表(QSS)

For a long time, Qt has allowed you to decorate your GUIs with CSS’ish style sheets. Inspired by the web, stylesheets are a great way to stylize your Qt GUI, but it seems that few people use them. In this tutorial, we’ll create an example dialog in Q

背景建模技术(七):预处理(PreProcessor)模块

预处理(PreProcessor)模块是BgsLibrary中一个必选的模块,是真正进入背景建模算法的"预处理"过程,其主要功能包括'去模糊'.'获得灰度图'.'应用Canny算子'等可选模块. 下面给出源码: #include "PreProcessor.h" namespace bgslibrary { PreProcessor::PreProcessor() : firstTime(true), equalizeHist(false), gaussianBlu

Platform Dependent Compilation

[Platform Dependent Compilation] 1.Platform Defines 2.在Project Setting -> Player 面板的Other Settings的Scripting Define Symbols可以自定义macro,在此片定义的macro会被所有代码引用. 以分号分隔. 3.You can define your own preprocessor directives to control which code gets included wh

(转载)Cross product

原文地址:https://en.wikipedia.org/wiki/Cross_product Cross product From Wikipedia, the free encyclopedia This article is about the cross product of two vectors in three-dimensional Euclidean space. For other uses, see Cross product (disambiguation). In m

Android NDK重定向std::cout输出到log

第一步,继承std::streambuf #include <iostream> #include <streambuf> class MyStreamBuf : public std::streambuf { enum { BUFFER_SIZE = 255, }; public: MyStreamBuf() { buffer_[BUFFER_SIZE] = '\0'; setp(buffer_, buffer_ + BUFFER_SIZE - 1); } ~MyStreamBu

A quick introduction to Source Insight for seamless development platform between Linux and Windows

前言 Source Insight是一个面向项目开发的程序编辑器和代码浏览器,它拥有内置的对C/C++, C#和Java等程序的分析.能分析源代码并在工作的同时动态维护它自己的符号数据库,并自动显示有用的上下文信息.越是大规模的代码,越能显示出SI的强大之处.Linux服务器开发领域,很多时候代码往往是从Linux主机上copy一份到Windows,然后SI为其创建项目.代码在Windows上修改好后,用WinSCP上传到Linux主机替换源代码,挥着使用Beyond Compare对比合并代码

深入V8引擎-初始化Platform之mac篇(1)

本来寻思着写一篇"'Hello' + ', World'"是怎么从JS代码编译然后输出的,然而compile过程的复杂性远超我的想象,强上怕会走火入魔,还是老老实实先回家种田,找点咸鱼方法先写着.虽然说是咸鱼方法,但是V8任何一块拿出来都不简单,之前讲的Time模块说实话大概是属于源码里面幼儿园级别的,这次试试难一点的. V8的源码在本地编译完成后,会提供一个hello-world.cc的sample,里面有新手用户标准的初始化流程,如下. int main(int argc, cha