在预处理时检测typedef [英] Detecting typedef at preprocessing time

查看:86
本文介绍了在预处理时检测typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,有一些包含以下内容的包含文件fh

行:


typedef unsigned int ui32;


我的问题是:如果我有一个包含fh的C源文件Fc,那么预处理器检测到ui32已经存在的时候是否可能是


预处理Fc?我们的想法是,如果和/或
只有当这样的typedef已经存在于Fc使用的某些包含文件中时,Fc才会输入如上所述的ui32

Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.

推荐答案



Thomas Carter写道:

Thomas Carter wrote:
想象一下,有一些包含以下
行的包含文件fh:
typedef unsigned int ui32;

我的问题是:如果我有一个包含fh的C源文件Fc,预处理器是否有可能检测到ui32已经存在,何时
预处理Fc?想法是Fc会像上面那样输入ui32,如果只有这样的typedef还没有在Fc使用的某些包含文件中
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.



/ *文件fh * /


#ifndef _UI32

#define _UI32

typedef unsigned int ui32;

# endif



/* File f.h */

#ifndef _UI32
#define _UI32
typedef unsigned int ui32 ;
#endif


文章< pa ************************* **@nanobots.com>,

Thomas Carter< T. ****** @ nanobots.com>写道:
In article <pa***************************@nanobots.com>,
Thomas Carter <T.******@nanobots.com> wrote:
想象一下,有一些包含以下
行的包含文件f.h:
typedef unsigned int ui32;
我的问题是:如果我有一个包含f.h的C源文件F.c,那么当预处理F.c时,预处理器是否有可能检测到ui32已经存在?这个想法是Fc会像上面那样输入ui32,如果只有这样的typedef不在Fc使用的某些包含文件中那么
Imagine that there is some include file f.h that contains the following
line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.



不,不是没有帮助,比如定义一个宏来表示该功能存在




预处理器的唯一货币是宏和算术

常量表达式。

-

原型是他们克隆的超类型。 - maplesoft


No, not without assistance such as defining a macro to indicate the
existance of the feature.

The only currencies of the preprocessor are macros and arithmetic
constant expressions.
--
Prototypes are supertypes of their clones. -- maplesoft


Thomas Carter schrieb:
Thomas Carter schrieb:
想象一下,有一些包含以下
行的包含文件fh:

typedef unsigned int ui32;

我的问题是:如果我有一个包含fh的C源文件Fc,预处理器是否有可能检测到ui32已经
预处理Fc时存在吗?想法是Fc会像上面那样输入ui32,如果只有这样的typedef不在Fc使用的某些包含文件中那么
Imagine that there is some include file f.h that contains the following
line:

typedef unsigned int ui32 ;

My question is: If I have a C source file F.c that includes f.h, is it
possible for the preprocessor to detect that ui32 already exists, when
preprocessing F.c? The idea is that F.c will typedef ui32 as above if and
only if such typedef is not already in some include file used by F.c.




预处理器本身不能解释typedef。


通常,你想只有一个这样的typedef。

因为你可以保护标题的内容免受多个

包含,通常的构造是


, - mytypes.h -

#ifndef MY_TYPES_H__

#define MY_TYPES_H__

.....

typedef unsigned int ui32;

.....

#endif

` ----


如果您需要ui32或其他类型的mytypes.h,那么

#include" mytypes.h"

如果任何标题也需要提供ui32,那么请确保它

也包括mytypes.h。


如果你处于罕见和不幸的境地,你可以

有一个实现typedef或者必须提供一个o f

你自己的,然后做同样的:通常有一个,可能是特定的b $ b实现,意味着检测类型是否已经

是那里。在标题mytypes.h中使用它。每当包含这个标题

时,你可以确定你有一个typedef或

另一个 - 但它就在那里。

通常更好:提供你自己的typedef名称。


示例:你需要一个至少24位的有符号整数类型和

决定你想先尝试是否< stdint.h>定义一个

typedef''ing your own:


一个解决方案:

, - mytypes.h -

#ifndef MY_TYPES_H__

#define MY_TYPES_H__

#include< limits.h>

#include< stdint .h>

.....

#ifndef INT_LEAST24_MAX

typedef签名long int_least24_t;

/ *为了正常化图片:提供限制* /

#define INT_LEAST24_MAX LONG_MAX

#define INT_LEAST24_MIN LONG_MIN

#endif

。 ....

#endif

` ----


另一个解决方案(我赞成的一个):

, - mytypes.h -

#ifndef MY_TYPES_H__

#define MY_TYPES_H__

#include< limits。 h>

#include< stdint.h>

.....

#ifdef INT_LEAST24_MAX

typedef int_least24_t sint24;

/ *可选:提供限制* /

#else

typedef签名长sint24;

/ *可选:提供限制* /

#endif

.....

#endif

`----

干杯

Michael

-

电子邮件:我的是/在/ gmx / dot / de address。



The preprocessor itself cannot interpret typedefs.

Usually, you want to have only one such typedef.
As you can protect the contents of a header against multiple
inclusion, the usual construct is

,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
.....
typedef unsigned int ui32;
.....
#endif
`----

If you need ui32 or another of the typed from "mytypes.h", then
#include "mytypes.h"
If any header needs to provide ui32, too, then make sure it
includes mytypes.h as well.

If you are in the rare and unfortunate situation that you can
either have an implementation typedef or have to provide one of
your own, then do the same: There usually is a, possibly
implementation specific, means to detect whether the type already
is there. Use this in your header mytypes.h. Whenever this header
is included, you can be sure that you have either one typedef or
the other -- but that it is there.
Usually better: Provide your own typedef name.

Example: You need a signed integer type with at least 24 bits and
decide you want to try first whether <stdint.h> defines one before
typedef''ing your own:

One solution:
,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
#include <limits.h>
#include <stdint.h>
.....
# ifndef INT_LEAST24_MAX
typedef signed long int_least24_t;
/* In order to "normalise" the picture: provide limits */
# define INT_LEAST24_MAX LONG_MAX
# define INT_LEAST24_MIN LONG_MIN
# endif
.....
#endif
`----

Another solution (one I would favour):
,-- mytypes.h --
#ifndef MY_TYPES_H__
#define MY_TYPES_H__
#include <limits.h>
#include <stdint.h>
.....
# ifdef INT_LEAST24_MAX
typedef int_least24_t sint24;
/* Optional: Provide limits */
# else
typedef signed long sint24;
/* Optional: Provide limits */
# endif
.....
#endif
`----
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于在预处理时检测typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆