multiple header files redefinition error in C

12,261

You can't redefine the same symbol in two different header files and include them together, you will have the redefinition of 'xxx' error you can see.

What you can do about that is either remove the typedef struct node from one of the files, or even better, move your struct node definition in another file, have that file protected from multiple inclusion using #ifndefs as you said, and include it in both "stackADT.h" and "queueADT.h"

For example :

myNode.h:

#ifndef MYNODE_H_
# define MYNODE_H_

typedef struct node
{
  char n;
  int  i;
}              QUEUE_NODE;

#endif

stackADT.h:

#include <...>
#include "myNode.h"
#include "..."

queueADT.h:

#include <...>
#include "myNode.h"
#include "..."

This way your .c source file can remain unchanged.

Share:
12,261
Hee Kyung Yoon
Author by

Hee Kyung Yoon

I love to code:D

Updated on June 04, 2022

Comments

  • Hee Kyung Yoon
    Hee Kyung Yoon almost 2 years

    When including multiple header files

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include "stackADT.h"
    #include "queueADT.h"
    

    redefinition error occurs

    In file included from graphTraverse3.c:10:
    ./queueADT.h:10:16: error: redefinition of 'node'
    typedef struct node
                   ^
    ./stackADT.h:9:16: note: previous definition is here
    typedef struct node
                   ^
    In file included from graphTraverse3.c:10:
    ./queueADT.h:69:20: warning: incompatible pointer types assigning to
          'QUEUE_NODE *' from 'struct node *' [-Wincompatible-pointer-types]
      ...queue->front = queue->front->next;
                      ^ ~~~~~~~~~~~~~~~~~~
    ./queueADT.h:93:16: warning: incompatible pointer types assigning to
          'QUEUE_NODE *' from 'struct node *' [-Wincompatible-pointer-types]
                    queue->front = queue->front->next;
                                 ^ ~~~~~~~~~~~~~~~~~~
    ./queueADT.h:117:23: warning: incompatible pointer types assigning to
          'struct node *' from 'QUEUE_NODE *' [-Wincompatible-pointer-types]
        queue->rear->next = newPtr;
                          ^ ~~~~~~
    3 warnings and 1 error generated.
    

    I tried attaching these, but it didn't work.

    #ifndef _STACK_H_
    #define _STACK_H_
    ....content....
    #endif
    

    Maybe it is only for C++.

    Added relevant header file parts.

    First queueADT.h

    #ifndef _QUEUE_H_
    #define _QUEUE_H_
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    // data type
    typedef struct node
      {
        void* dataPtr;
        struct node* next;
      } QUEUE_NODE;
    

    and this is stackADT.h.

    #ifndef _STACK_H_
    #define _STACK_H_
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    // Structure Declarations
    typedef struct nodeS
      {
      void* dataPtr;
      struct nodeS* link;
      } STACK_NODE;
    
    • Eregrith
      Eregrith almost 9 years
      You can't redefine the same symbol in two different header files. Remove one of the typedef struct node.
    • Eregrith
      Eregrith almost 9 years
      Or even better yet: Move your definition of your struct node and its typedefto another file dedicated to doing that.
    • Admin
      Admin almost 9 years
      You might want to show the relevant lines from those header files.
  • Hee Kyung Yoon
    Hee Kyung Yoon almost 9 years
    Can I ask why #ifndef method to queueADT.h and stackADT.h doesn't work while it works to the header file(mynode_h) included in these two files?
  • Eregrith
    Eregrith almost 9 years
    Depending on around what you put the #ifndef and the name of the macro you use, it might do nothing to prevent the compiler to include twice the line causing the problem. When you put it around the actual declaration of the struct, it makes sure it will not be declared twice.
  • Eregrith
    Eregrith almost 9 years
    @HeeKyungYoon It would solve the problem to put the same #ifndefs around your declarations in both files, but it violates the dry principle and might be quickly undone if you ever change only one of the #ifndef macro name ;)
  • Hee Kyung Yoon
    Hee Kyung Yoon almost 9 years
    Thanks for the detailed answer:D Then, is the 'mynode.h' method is a convention when dealing with multiple header files? Although it solves the problem, I think that later it might be confusing to have another file.
  • Eregrith
    Eregrith almost 9 years
    @HeeKyungYoon Usually I use a .h file per .c file and one .h file for all structs and other typedefs, #defines and so on.