Home > Software engineering >  Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99
Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99

Time:07-07

I am observing below MISRA Warnings.

[L] Number of macro definitions exceeds 4095 - program does not conform strictly to ISO:C99. MISRA - 2012, Message Identifier : 0380

Code line:

#include "COMH_ExteriorLightUI.h"

Do we have any limit on number of MACRO defination to be used in code according to MISRA rules ?

I am getting this error while trying to include header file.

CodePudding user response:

The C language (C17 5.2.4.1) only guarantees that 4095 different macro identifiers in a single translation unit are supported. If you have more macros than that, your code is non-portable and may not compile.

You can only solve this by better program design, by splitting huge .c files into several and localize macros that don't need to be exposed outside that .h/.c file pair.

For example, you could have a public header, which is implemented in two .c files where one .c file contains the function definitions for the public API and the other .c file contains internal functions. Have this second, private .c file include it's own .h file with macros that the caller need not know about, or alternatively place the macros inside that private .c file.

Also, avoid a somewhat common but very bad practice of creating a "super header" which in turn includes every other header file in the project. Not only does that risk blow the preprocessor, it also creates a tight coupling between every single, unrelated file in the project. Such a design is completely unacceptable for critical systems.

  • Related