This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Custom Compiler Error Message using #error

Hello!

In case there is a post about this im sorry, i searched this forum and the wiki for quite some time now and couldnt find aynthing on this.

There are some constants in my code that are used to set options. Most of the options are not allowed to exceed a defined range of values. The code will be used by others as well, so I am trying to implement custom compiler errors giving a warning when a value that is not allowed has been chosen.


I checked the manual of the compiler as well, p103 of http://www.ccsinfo.com/downloads/ccs_c_manual.pdf shows how to have a custom compiler error generated:

#if BUFFER_SIZE>16
#error Buffer size is too large
#endif

This is exactly what i want, but when i add something like this to my project it doesnt have any effect.

What i did is:

const int Meas_Oversampling = 5;

#if Meas_OverSampling>4 || Meas_OverSampling<0
#error Meas_OverSampling: allowed values 0..4
#endif

compiles fine, anything I am missing there?

Greetings,

Lars

  • A preprocessor condiitional statement like ...

    Lars Heinrichs said:
    #if Meas_OverSampling>4 || Meas_OverSampling<0

    only works with preprocessor symbol names created with #define statements.  This ...

    Lars Heinrichs said:
    const int Meas_Oversampling = 5;

    ... doesn't count.  Rewrite it as ...

    #define Meas_OverSampling 5
    

    Note that when a #if statement sees a preprocessor symbol name that has no definition, it presumes it is zero.  That is why your #if evaluates to false.

    Thanks and regards,

    -George