C - 跨平台宏定义

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - 跨平台宏定义相关的知识,希望对你有一定的参考价值。

  1 #ifndef _H_INCLUDED
  2 #define _H_INCLUDED
  3 
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <stdarg.h>
  7 #include <float.h>
  8 #include <string.h>
  9 #include <math.h>
 10 #include <limits.h>
 11 #include <direct.h>
 12 
 13 #define DEBUG
 14 #define USE_WINDOWS
 15 
 16 /*********************************************/
 17 #if defined(USE_WINDOWS)
 18 #define DIR_SEPERATOR ‘\\‘
 19 #else
 20 #define DIR_SEPERATOR ‘/‘
 21 #endif
 22 /*********************************************/
 23 
 24 /*********************************************/
 25 #if LONG_MAX >> 31 > 0
 26 #define WORD_BYTES 8     /* 64-bit */
 27 #elif INT_MAX >> 15 > 0
 28 #define WORD_BYTES 4     /* 32-bit */
 29 #else
 30 #define WORD_BYTES 2     /* 16-bit */
 31 #endif
 32 /*********************************************/
 33 
 34 /*********************************************/
 35 #if WORD_BYTES == 8
 36 
 37 typedef signed long intx;
 38 typedef unsigned long uintx;
 39 typedef signed int int32;
 40 typedef unsigned int uint32;
 41 
 42 #elif WORD_BYTES == 4
 43 
 44 typedef signed int intx;
 45 typedef unsigned int uintx;
 46 typedef signed int int32;
 47 typedef unsigned int uint32;
 48 
 49 #else
 50 
 51 typedef signed int intx;
 52 typedef unsigned int uintx;
 53 typedef signed long int32;
 54 typedef unsigned long uint32;
 55 
 56 #endif /* WORD_BYTES */
 57 /*********************************************/
 58 
 59 /*********************************************/
 60 #define REAL_TYPE_FLOAT            1
 61 #define REAL_TYPE_DOUBLE         2
 62 #define REAL_TYPE_LONGDOUBLE    3
 63 /*********************************************/
 64 
 65 /*********************************************/
 66 #ifndef REAL_TYPE
 67 
 68 #if WORD_BYTES == 8
 69 #define REAL_BYTES     8
 70 #define REAL_TYPE     REAL_TYPE_DOUBLE
 71 #else
 72 #define REAL_BYTES     4
 73 #define REAL_TYPE     REAL_TYPE_FLOAT
 74 #endif /* WORD_BYTES */
 75 
 76 #endif /* REAL_TYPE */
 77 /*********************************************/
 78 
 79 /*********************************************/
 80 #if REAL_TYPE == REAL_TYPE_FLOAT
 81 
 82 typedef float real;
 83 #define REAL_FORMAT        "%.7g"
 84 #define real_limit(n)     FLT_##n
 85 #define math_op(op)     op##f
 86 #define math_s2r(s,p)    (strtof((s),(p)))
 87 
 88 #elif REAL_TYPE == REAL_TYPE_DOUBLE
 89 
 90 typedef double real;
 91 #define REAL_FORMAT        "%.14g"
 92 #define real_limit(n)     DBL_##n
 93 #define math_op(op)        op##d
 94 #define math_s2r(s,p)    (strtod((s),(p)))
 95 
 96 #elif REAL_TYPE == REAL_TYPE_LONGDOUBLE
 97 
 98 #define REAL_FORMAT        "%.19g"
 99 #define real_limit(n)     LDBL_##n
100 #define math_op(op)        op##l
101 #define math_s2r(s,p)    (strtold((s),(p)))
102 
103 #else
104 
105 #error "numeric float type not defined"
106 
107 #endif
108 /*********************************************/
109 
110 /*********************************************/
111 typedef signed short int16;
112 typedef unsigned short uint16;
113 typedef signed char int8;
114 typedef unsigned char uint8;
115 typedef unsigned char byte;
116 typedef unsigned char boolean;
117 typedef void *Pointer;
118 typedef char *Chars;
119 typedef FILE *File;
120 /*********************************************/
121 
122 /*********************************************/
123 #define bitsof(t) (sizeof(t)<<3)
124 #define cast_type(t, x) ((t)(x))
125 #define cast_intx(x) ((intx)(x))
126 #define cast_uintx(x) ((uintx)(x))
127 #define cast_int32(x) ((int32)(x))
128 #define cast_uint32(x) ((uint32)(x))
129 #define cast_int16(x) ((int16)(x))
130 #define cast_uint16(x) ((uint16)(x))
131 #define cast_int8(x) ((int8)(x))
132 #define cast_uint8(x) ((uint8)(x))
133 #define cast_byte(x) ((byte)(x))
134 #define cast_boolean(x) ((boolean)(!!(x)))
135 #define cast_void(x) ((void)(x))
136 #define cast_Chars(x) ((Chars)(x))
137 #define cast_Pointer(x) ((Pointer)(x))
138 #define struct_new(t) ((t)calloc(1, sizeof_type(t)))
139 #define struct_alloc(t) ((t)malloc(sizeof_type(t)))
140 #define array_size(a) (sizeof(a)/sizeof(*(a)))
141 #define math_max(x, y) ((x) > (y) ? (x) : (y))
142 #define math_min(x, y) ((x) < (y) ? (x) : (y))
143 #define math_swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
144 #define math_abs(x) ((x) < 0 ? -(x) : (x))
145 #define math_sign(x) ((x) < 0 ? -1 : ((x) ? 1 : 0))
146 #define math_r2i(r, i) ((i = (r)) == (r))
147 #define math_pow2(n) (1 << (n))
148 /*********************************************/
149 
150 /*********************************************/
151 #ifdef DEBUG
152 #define debug_assert(c) ((c)||printf("debug>> assertion failed: %s, #%d, "#c"\n",__FILE__,__LINE__))
153 #else
154 #define debug_assert(c) ((void)0)
155 #endif
156 /*********************************************/
157 
158 #endif

 

以上是关于C - 跨平台宏定义的主要内容,如果未能解决你的问题,请参考以下文章

预定义宏,C语言预定义的宏详解

2C++ 的升级

iOS 7:漫谈#define 宏定义(转)

[转]C语言宏定义技巧

eCos的HAL接口采用宏定义形式的优势

C语言文件名行号函数名的方法