如何检查uint8_t是否以类型而不是以unsigned char形式存在? [英] How to check if uint8_t exists as a type, instead of unsigned char?
问题描述
我有两个编译器,一个可以识别uint8_t(GCC ARM-EABI),另一个不能(瑞萨M16标准工具链)。
I have two compilers, one that recognizes uint8_t(GCC ARM-EABI), and one that doesn't(Renesas M16 Standard Toolchain).
瑞萨工具链不符合ANSI C,因此您可以扔掉。因此,uint8_t,uint16_t等没有定义为现有类型。
The Renesas Toolchain is NOT ANSI C compliant, so you can throw out . So uint8_t, uint16_t,... aren't defined as existing types.
为了保持可移植性,我想使用相同的类型(最好是uint8_t,因为
In order to maintain portability, I would like to have the same types(preferably uint8_t, due to the ambiguity of int).
我的平台也是不同大小的处理器(ARM是32位,而瑞萨是16位)。导致int成为不同的值。
Also my platforms are different size processors(ARM is 32 bit, and Renesas is 16 bit). Causing int to be different values.
是否可以检查uint8_t是否作为一种类型存在?
Is there a way to check if uint8_t exists as a type?
如果不是,则将其(以及其他uint16_t,uint32_t等)声明为类型?
And if not, declare it(and others uint16_t, uint32_t,...) as a type?
推荐答案
有一些不同的处理方式。在需要可移植的开源项目中,常见的解决方案是使用配置脚本,运行该脚本以设置构建系统。然后,它会在某些 config.h
或类似的文件中设置或未设置类似 HAVE_UINTX_TYPES
之类的东西。 配置脚本的结果,然后执行以下操作:
There are a few different ways to deal with this. In an open source project that needs to be portable, the common solution is to have a "configure script", which is run to setup the build system. It would then have something like HAVE_UINTX_TYPES
that is set or not set in some config.h
or similar [which was one of the results of the "configure script", and do something like this:
#include "config.h"
...
#ifndef HAVE_UINTX_TYPES
#include "uintx_types.h"
#endif
在不太需要在几乎任何东西上运行的系统中,只需将 -DHAVE_UINTX_TYPES
作为标志的一部分,即可解决相同的问题。到编译器。而且,由于您(大概)在构建系统中有一部分为两个不同的构建设置了不同的编译选项,选择了不同的编译器等,因此添加这个问题不应该成为大问题。
In a less "needs to run on almost anything" system, you could solve the same problem by simply have a -DHAVE_UINTX_TYPES
as part of the flags to the compiler. And since you (presumably) have some part of the build system that sets different compile options, picks a different compiler, etc, for the two different builds, this shouldn't be a big issue to add.
并且假设您很高兴您的未签名字符
确实是8位,那么您也可以将 uintx_types.h
包含以下内容:
And assuming that you are happy that your unsigned char
is indeed 8 bits, you could also do have a uintx_types.h
that contains something like this:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
另一种选择是不使用 uint8_t
和 uint16_t
等,但是有您自己的定义[并且这些定义取决于是ARM还是瑞萨电子的适当构建设置,例如通过使用不同的包含选项]:
Another option is to not use uint8_t
and uint16_t
etc directly, but have your own definitions [and have these depend on the appropriate build setting for "is it ARM or Renesas", e.g. by using different include options]:
ARM / types.h:
ARM/types.h:
typedef unsigned char u_int8;
typedef unsigned short u_int16;
typedef unsigned int u_int32;
Renesas / types.h:
Renesas/types.h:
typedef unsigned char u_int8;
typedef unsigned int u_int16;
typedef unsigned long u_int32;
这篇关于如何检查uint8_t是否以类型而不是以unsigned char形式存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!