[C] 让VC支持C99的整数类型V1.01。避免包含目录问题,更名auto

[C] 让VC支持C99的整数类型V1.01。避免包含目录问题,更名auto_stdint.h、auto_inttypes.h(在VC6至VC2012、GCC、BCB等编译器下测试通过)

作者:zyl910

  以前我曾为了让VC++等编译器支持C99的整数类型,编写了同名的stdint.h、inttypes.h来智能处理()。现在将其升级到v1.01版。

一、改动说明1.1 包含目录问题

  在1.00版,我编写的头文件与系统头文件同名,利用“#include “XXX””与“#include <XXX>”的区别,香港空间,使其智能使用系统头文件。  这样做的优点是基本不需改动代码(只需将“#include <stdint.h>”改为“#include “stdint.h””),而且易读性好,一看就知道是C99整数类型。

  后来使用时发现该方案存在包含目录问题——我的stdint.h、inttypes.h不能放在项目include目录中。  这是因为“#include <XXX>”时会优先检查项目include目录,而后才是系统include目录。  如果将我的stdint.h、inttypes.h放在项目include目录中,会导致循环引用,无法定位到系统头文件。  所以,只能将我的stdint.h、inttypes.h放在项目src目录。  当项目较大时,会建立多级子目录来存放源码。这时只有将stdint.h、inttypes.h复制到各个子目录中,管理起来不方便。

  于是我决定将我的stdint.h、inttypes.h分辨改名为auto_stdint.h、auto_inttypes.h,美国服务器,这样就可以放在项目include目录中了。缺点是使用时麻烦一点,香港服务器,要包含auto_stdint.h、auto_inttypes.h。

  既然文件改名了,宏也要改名——__AUTO_STDINT_H_INCLUDED(原_STDINT_H_ALL_)。__AUTO_STDINT_H_USESYS(原_STDINT_H_SYS_)。__AUTO_INTTYPES_H_INCLUDED(原_INTTYPES_H_ALL_)。__AUTO_INTTYPES_H_USESYS(原_INTTYPES_H_SYS_)。

1.2 编译器兼容性

  测试了 Visual C++ 2008。发现它果然不支持stdint.h与inttypes.h。  测试了 Visual C++ 2012。发现它支持stdint.h,仍不支持inttypes.h。

二、全部代码

  文件清单——auto_inttypes.hauto_stdint.hc99int.cc99int.dspc99int.dswc99int_2003.slnc99int_2003.vcprojc99int_2005.slnc99int_2005.vcprojc99int_2008.slnc99int_2008.vcprojc99int_2010.slnc99int_2010.vcxprojc99int_2010.vcxproj.filtersc99int_2010.vcxproj.userc99int_2012.slnc99int_2012.vcxprojc99int_2012.vcxproj.filtersc99int_bcb.bpfc99int_bcb.bprc99int_bcb.resmakefile

2.1 auto_stdint.h

  全部代码——

/////////////////////////////////////////////////////////////*auto_stdint.h: 兼容C99标准的stdint.hAuthor: zyl910Blog: URL: Version: V1.01Updata: 2013-01-10测试过的编译器–VC: 6, 2003, 2005, 2008, 2010, 2012.BCB: 6.GCC(Linux): 4.7.0(Fedora 17).GCC(Mac): llvm-gcc-4.2(Mac OS X Lion 10.7.4, Xcode 4.4.1).GCC(MinGW): 4.6.2(MinGW(20120426)), 4.7.1(TDM-GCC(MinGW-w64)).Update~~~~~~[2013-01-01] V1.01* 检查了对VC2008、VC2012的兼容性. 确认VC2008不支持stdint.h.* 为了避免包含目录问题,更名auto_stdint.h(原stdint.h).* 更改宏名:__AUTO_STDINT_H_INCLUDED(原_STDINT_H_ALL_),__AUTO_STDINT_H_USESYS(原_STDINT_H_SYS_).[2012-08-08] V1.0* V1.0发布.* 参考了 msinttypes-r26. * 修正了 VC6编译C++程序时wchar.h会报错 问题.*/////////////////////////////////////////////////////////////#ifndef __AUTO_STDINT_H_INCLUDED#define __AUTO_STDINT_H_INCLUDED__AUTO_STDINT_H_USESYS__AUTO_STDINT_H_USESYS_MSC_VER >=1600 __AUTO_STDINT_H_USESYSdefined(__BORLANDC__) __BORLANDC__ >=0x0560 __AUTO_STDINT_H_USESYS_INTTYPES_H_SYS_ #ifdef __AUTO_STDINT_H_USESYS// 使用编译器提供的<stdint.h>#include <stdint.h>#ifndef _MSC_STDINT_H_ _MSC_STDINT_H_#include <limits.h>// For Visual Studio 6 in C++ mode and for many Visual Studio versions when// compiling for ARM we should wrap <wchar.h> include with ‘extern “C++” {}’// or compiler give many errors like this:// error C2733: second C linkage of overloaded function ‘wmemchr’ not allowed//#ifdef __cplusplus//extern “C” {//#endif//# include <wchar.h>//#ifdef __cplusplus//}//#endif// <zyl910>: 在VC6下测试时, 发现上面的方法会报告很多C2733错误. 还是直接include算了.#include <wchar.h>// Define _W64 macros to mark types changing their size, like intptr_t.#ifndef _W64# if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300#define _W64 __w64# else#define _W64# endif#endif// 7.18.1 Integer types// 7.18.1.1 Exact-width integer types// Visual Studio 6 and Embedded Visual C++ 4 doesn’t// realize that, e.g. char has the same size as __int8(_MSC_VER < 1300)typedef signed charint8_t;typedef signed shortint16_t;typedef signed intint32_t;typedef unsigned charuint8_t;typedef unsigned short uint16_t;typedef unsigned intuint32_t;#elsetypedef signed __int8int8_t;typedef signed __int16 int16_t;typedef signed __int32 int32_t;typedef unsigned __int8 uint8_t;typedef unsigned __int16 uint16_t;typedef unsigned __int32 uint32_t;#endiftypedef signed __int64int64_t;typedef unsigned __int64uint64_t;// 7.18.1.2 Minimum-width integer typestypedef int8_t int_least8_t;typedef int16_t int_least16_t;typedef int32_t int_least32_t;typedef int64_t int_least64_t;typedef uint8_t uint_least8_t;typedef uint16_t uint_least16_t;typedef uint32_t uint_least32_t;typedef uint64_t uint_least64_t;// 7.18.1.3 Fastest minimum-width integer typestypedef int8_t int_fast8_t;typedef int16_t int_fast16_t;typedef int32_t int_fast32_t;typedef int64_t int_fast64_t;typedef uint8_t uint_fast8_t;typedef uint16_t uint_fast16_t;typedef uint32_t uint_fast32_t;typedef uint64_t uint_fast64_t;// 7.18.1.4 Integer types capable of holding object pointers#ifdef _WIN64 // [ typedef signed __int64 intptr_t;typedef unsigned __int64 uintptr_t;typedef _W64 signed int intptr_t;typedef _W64 unsigned int uintptr_t;typedef int64_t intmax_t;typedef uint64_t uintmax_t;!defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) INT8_MIN((int8_t)_I8_MIN)#define INT8_MAX_I8_MAX#define INT16_MIN ((int16_t)_I16_MIN)#define INT16_MAX _I16_MAX#define INT32_MIN ((int32_t)_I32_MIN)#define INT32_MAX _I32_MAX#define INT64_MIN ((int64_t)_I64_MIN)#define INT64_MAX _I64_MAX#define UINT8_MAX _UI8_MAX#define UINT16_MAX _UI16_MAX#define UINT32_MAX _UI32_MAX#define UINT64_MAX _UI64_MAXINT_LEAST8_MIN INT8_MIN#define INT_LEAST8_MAX INT8_MAX#define INT_LEAST16_MIN INT16_MIN#define INT_LEAST16_MAX INT16_MAX#define INT_LEAST32_MIN INT32_MIN#define INT_LEAST32_MAX INT32_MAX#define INT_LEAST64_MIN INT64_MIN#define INT_LEAST64_MAX INT64_MAX#define UINT_LEAST8_MAX UINT8_MAX#define UINT_LEAST16_MAX UINT16_MAX#define UINT_LEAST32_MAX UINT32_MAX#define UINT_LEAST64_MAX UINT64_MAXINT_FAST8_MIN INT8_MIN#define INT_FAST8_MAX INT8_MAX#define INT_FAST16_MIN INT16_MIN#define INT_FAST16_MAX INT16_MAX#define INT_FAST32_MIN INT32_MIN#define INT_FAST32_MAX INT32_MAX#define INT_FAST64_MIN INT64_MIN#define INT_FAST64_MAX INT64_MAX#define UINT_FAST8_MAX UINT8_MAX#define UINT_FAST16_MAX UINT16_MAX#define UINT_FAST32_MAX UINT32_MAX#define UINT_FAST64_MAX UINT64_MAX// 7.18.2.4 Limits of integer types capable of holding object pointers#ifdef _WIN64 // [# define INTPTR_MIN INT64_MIN# define INTPTR_MAX INT64_MAX# define UINTPTR_MAX UINT64_MAX# define INTPTR_MIN INT32_MIN# define INTPTR_MAX INT32_MAX# define UINTPTR_MAX UINT32_MAXINTMAX_MIN INT64_MIN#define INTMAX_MAX INT64_MAX#define UINTMAX_MAX UINT64_MAX// 7.18.3 Limits of other integer types#ifdef _WIN64 // [# define PTRDIFF_MIN _I64_MIN# define PTRDIFF_MAX _I64_MAX# define PTRDIFF_MIN _I32_MIN# define PTRDIFF_MAX _I32_MAXSIG_ATOMIC_MIN INT_MIN#define SIG_ATOMIC_MAX INT_MAX#ifndef SIZE_MAX // [# ifdef _WIN64 // [#define SIZE_MAX _UI64_MAX# #define SIZE_MAX _UI32_MAX# endif #ifndef WCHAR_MIN // [# define WCHAR_MIN #ifndef WCHAR_MAX // [# define WCHAR_MAX _UI16_MAXWINT_MIN 0#define WINT_MAX _UI16_MAX!defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) INT8_C(val) val##i8#define INT16_C(val) val##i16#define INT32_C(val) val##i32#define INT64_C(val) val##i64#define UINT8_C(val) val##ui8#define UINT16_C(val) val##ui16#define UINT32_C(val) val##ui32#define UINT64_C(val) val##ui64INTMAX_C INT64_C#define UINTMAX_C UINT64_C

2.2 auto_inttypes.h

  全部代码——

造物之前,必先造人。

[C] 让VC支持C99的整数类型V1.01。避免包含目录问题,更名auto

相关文章:

你感兴趣的文章:

标签云: