Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't solve: 'Redefinition of NULL macro'

Tags:

c

I'm working on a big project and I have this warning:

...\include\stddef.h" 38/9] macro "NULL" redefined
...\ncs_types.h" 125/13] previous definition of macro "NULL"

In the first file stddef.h which comes from a compiler installation folder I have this:

#ifdef  __cplusplus
#define NULL    (0)
#else   /* defined(__cplusplus) */
#define NULL    ((void *) 0)
#endif  /* defined(__cplusplus) */

I'm not allowed to modify this file.

Now in the other file which is part of the application code(which I'm allowed to modify. So I have to get rid of this warning by doing something in the file from the application code and don't touch the other one) I have this:

/* NULL pointer definition if not already defined */
#ifndef NULL
    /* Deviation MISRA-2 */
    #define NULL   0
#endif

And then I get that warning. I cannot get rid of it. I have tried things such as:
- undefining the macro in my file from the application -> same warning
- including an #ifndef guard -> same warning
- undefining the macro in my application file if '__cplusplus' is not defined - > same warning
- in my application file I have tried defining the macro to void pointer type instead of pure '0'.
Like this:

#ifndef NULL
    /* Deviation MISRA-2 */
    #define NULL    ((void *) 0)
#endif

From what I know if you redefine a macro to the same thing that it was already defined there should be no warning from the compiler. However, if I do this I get an incompatible types at argument error somewhere in the project. Apparently I need the NULL macro to be defined as '0' and not casted to a void pointer type.

Please if possible help me understand why I get that warning and how can I remove it. Also please help me understand this: If in my application file I have the #ifndef NULL...guard why is the macro getting redefined anyway(since it is already defined in the header file from the compiler)? Shouldn't the preprocessor skip that line that redefines NULL? I'm very confused about this.

Also one note: I'm getting this warning not once but several times when I build my project. Same warning about the same two files. I guess it has something to do about how the files are included in the build script. Please give me some ideas on what to investigate to understand the problem.

Thank you very much for reading my long post ☺. I'm sorry I cannot give further details as this is a work related project.

like image 218
Cantaff0rd Avatar asked Mar 04 '23 05:03

Cantaff0rd


2 Answers

...\include\stddef.h" 38/9] macro "NULL" redefined
...\ncs_types.h" 125/13] previous definition of macro "NULL"

This is telling you that NULL is initially defined in ncs_types.h and then redefined in stddef.h. The ordering of the error messages is a little confusing.

You have three options:

  1. Don't define NULL in your header file at all. It'll be defined later, by stddef.h. This probably won't work if you're using NULL in that header.
  2. Include stddef.h before your header file.
  3. Include stddef.h in your header file, instead of defining NULL.
like image 189
Roger Lipscombe Avatar answered Mar 19 '23 03:03

Roger Lipscombe


Change

/* NULL pointer definition if not already defined */
#ifndef NULL
    /* Deviation MISRA-2 */
    #define NULL   0
#endif

into

#include <stddef.h>
  1. You have the error because your file is included before stddef.h. So first your file defines NULL, then stddef.h defines null, because the order is important.
  2. Don't define NULL yourself. Just include stddef.h file. It has the NULL symbol waiting for you.
like image 30
KamilCuk Avatar answered Mar 19 '23 02:03

KamilCuk