0

I need to define all structs in one head file but how can I do that, in order to avoid the redefinition of typedef error !

melpomene
  • 79,257
  • 6
  • 70
  • 127
Alsamman
  • 51
  • 5
  • Hmmm ... define/declare them in alphabetical order? – pmg Jul 30 '19 at 10:06
  • What "redefinition of typedef error"? Are you talking about structs or typedefs? – melpomene Jul 30 '19 at 10:10
  • Do you mean, when the file is included you get errors because the structs are re-defined? If that's what you mean take a look [here](https://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files) – Federico klez Culloca Jul 30 '19 at 10:14
  • 5
    Why and how are you getting redefinition errors? Post some actual code that demonstrates those errors. – Andrew Henle Jul 30 '19 at 10:21

2 Answers2

0

If your problem is the redefinition of elements with multiple header inclusions, you can use the preprocessor macros #ifndef and #define to only define once each value.

cocool97
  • 1,145
  • 5
  • 20
0

do you mean that you want to separate your program file into one header file that declares the struct you need and one program file that include your program?

create a file named "type.h", and in this file

/* type.h */
#ifndef TYPE_H
#define TYPE_H
typedef struct t_Node
{
  int m_Info;
}t_Node;

/* ... definition of another struct */
#endif

and in the file that includes your program, for example, "main.c"

#include <stdio.h>
#include  "type.h"
int main(void)
{
  /* your program */
}

and you need to put the two files in one directory. Or you need to use gcc -c main.c -I <directory of type.h>