Generating Pre-Compiler Error

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

I have got the following define/macro:

#define NVRAM__END_ADDRESS_CBIT_HISTORY				(NVRAM__START_ADDRESS_CBIT_HISTORY + CBIT__NUMBER_OF_CBIT_HISTORY_EVENTS*sizeof(t_CBIT_History))

I would like the compiler to generate an error for me if NVRAM__END_ADDRESS_CBIT_HISTORY gets bigger than a certain value (in this case 0x7FFF).

Is there a way to do this?

Thanks
Jacques

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#if ((NVRAM__END_ADDRESS_CBIT_HISTORY) > 0x7FFF) 
#warning duff history
#error duff history
#endif

You can never use too many parentheses. The compilation will stop with the #error. A #warning just informs.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, thanks for your reply, where must I put the code , in a function or like after my #include?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Imagine that you are a Compiler.

You always start knowing nothing.
As you read a file, including stuff as you go, you gradually learn about what textual substitutions to do.

Your expression can only be evaluated when all the 'components' have some values. i.e. a legal arithmetic or logical expression.

The C compiler can NEVER look ahead.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, is see your point. I include it after the define but I get the following error when compiling:

hwdef.h:133:7: error: missing binary operator before token "("

When I replace the "sizeof" statement with for example 10, then the warning and errors works.

How can I get it to work with the "sizeof" statement?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't.

The pre-processor knows nothing about sizeof() operator.
Only the C compiler proper knows sizeof().

The pre-processor only really does textual substitution. However it can do simple arithmetic, and will even do it in floating point if you want. The arithmetic is only ever actually used in your #if (expression)

You have to do your conditional compilation with some other test.

If you explain exactly what you want to do, someone will know what to do.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have the following for computing EEPROM usage:

/* The following definitions are needed to compute the total EEPROM usage in
 * each module.  This is used to be sure we don't overlap one modules EEPROM
 * section with another's (yes, it has happened!).
 */
#define SIZEOF_UINT8_T   1
#define SIZEOF_INT8_T    1
#define SIZEOF_UINT16_T  2
#define SIZEOF_INT16_T   2
#define SIZEOF_PORTSHORT 2

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

#define SIZEOF_UINT8_T 1
#define SIZEOF_INT8_T 1
#define SIZEOF_UINT16_T 2
#define SIZEOF_INT16_T 2
#define SIZEOF_PORTSHORT 2

Stu just wondering but did you know that you can get these from the compiler:

D:\test>avr-gcc -mmcu=atmega16 -E -dM test.c | grep SIZEOF
#define __SIZEOF_INT__ 2
#define __SIZEOF_POINTER__ 2
#define __SIZEOF_LONG__ 4
#define __SIZEOF_LONG_DOUBLE__ 4
#define __SIZEOF_SIZE_T__ 2
#define __SIZEOF_WINT_T__ 2
#define __SIZEOF_PTRDIFF_T__ 2
#define __SIZEOF_FLOAT__ 4
#define __SIZEOF_SHORT__ 2
#define __SIZEOF_WCHAR_T__ 2
#define __SIZEOF_DOUBLE__ 4
#define __SIZEOF_LONG_LONG__ 8

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My sizeof is for a structure. So I guess I have to remove the sizeof in put in manually the size in bytes. Only future problem is that if I change the structured I will forget to change the number of bytes in the define!

Thanks for your replies and help!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can obviously check the CONSTANT with the sizeof() in the compiler proper. e.g. print an error or light an LED.

The compiler will only generate the code if you have an inequality. e.g.

if (sizeof(MY_TYPE) != WHAT_I_THINK) {
    printf("this only gets generated if expression is true");
}

The optimiser sees at compile time that this branch can never be taken.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff wrote:
Stu just wondering but did you know that you can get these from the compiler...
Obviously not, or I would not have defined my own!

(thanks! :oops: )

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!