CW 4.5 for s12x - #if and sizeof

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

CW 4.5 for s12x - #if and sizeof

跳至解决方案
2,595 次查看
calou
Contributor III
hello,
I use CW 4.5 for s12x
I would like to display an message error if the struct is  too much important:
 
typedef struct
{
 //tensions de références vref1 doit tjrs commencer la structure
 SWORD vref1;
 SWORD vref2;
 SWORD vref3;
 //config des switchs
 SWORD swcfg;
 //variable pour tester si l'eeprom a été configurée ou non
 //0=>non configuré 1=>configurée
 SWORD etat; 
}DATA_CONFIG;
_EEPROM_H_ DATA_CONFIG Config;
 
#if (sizeof(DATA_CONFIG)>2000)
#error "error message"
#endif
 
I have the message Undefined Macro 'sizeof'
How could solve this problem?
 
Thank you for help
 
Alban Edit: s'te plait indique le produit dans le sujet.

Message Edited by Alban on 2007-03-14 03:37 PM

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,508 次查看
CompilerGuru
NXP Employee
NXP Employee
The ANSI-C language does clearly separate the preprocessing from the C language parsing. The sizeof (types, stucts, variables, functions, enums,...) elements are all parts of the C language, during preprocessing they are identifiers like any other too.
I know some compilers (gnu and others) do support to use some features like sizeof in the preprocessor as language extension, but this extension is not available in CW for HC12 V4.5.

I don't have a really clean solution for you, but at least a solution. The point is that you can cause the compiler to generate an error both a preprocess time, as you tried, or at "compile" (say C parsing) time. During the parsing, things like sizeof are known. The reason why it is not so clean is that there is no equivalent to the preprocessor #error directive, so I'm usually "abusing" some other condition in the language to get the compile time test.

A example, to give an idea.
extern char TestCondition[2];
#define COMPILE_TIME_CHECK(cond) extern char TestCondition[(cond)+1];
COMPILE_TIME_CHECK(sizeof(int) == 3);

downside: using some other error message, unless you look at the location from where the error happens, the message maybe confusing.
upside: it's the ANSI-C compliant version.

Daniel

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,509 次查看
CompilerGuru
NXP Employee
NXP Employee
The ANSI-C language does clearly separate the preprocessing from the C language parsing. The sizeof (types, stucts, variables, functions, enums,...) elements are all parts of the C language, during preprocessing they are identifiers like any other too.
I know some compilers (gnu and others) do support to use some features like sizeof in the preprocessor as language extension, but this extension is not available in CW for HC12 V4.5.

I don't have a really clean solution for you, but at least a solution. The point is that you can cause the compiler to generate an error both a preprocess time, as you tried, or at "compile" (say C parsing) time. During the parsing, things like sizeof are known. The reason why it is not so clean is that there is no equivalent to the preprocessor #error directive, so I'm usually "abusing" some other condition in the language to get the compile time test.

A example, to give an idea.
extern char TestCondition[2];
#define COMPILE_TIME_CHECK(cond) extern char TestCondition[(cond)+1];
COMPILE_TIME_CHECK(sizeof(int) == 3);

downside: using some other error message, unless you look at the location from where the error happens, the message maybe confusing.
upside: it's the ANSI-C compliant version.

Daniel
0 项奖励
回复
1,508 次查看
calou
Contributor III
Thank you Daniel for your help
 
Regards
0 项奖励
回复