Take the 2-minute tour ×

I need to define a struct, let's say

typedef struct _GlobalStruct {
    int a, b;
} GlobalStruct

and then use GlobalStruct wherever I want just by including globalstruct.h

For example:

#include globalstruct.h
void test(GlobalStruct *gs){...}

How can I do this?



I think I need to clarify a little bit more my question as I'm completely stuck.

#include "gstruct.h"
#include "a.h"
#include "b.h"

void something(GlobalStruct *gs){...}


#ifdef gstruct_h
#define gstruct_h
typedef struct _GlobalStruct{
    int a, b;
} GlobalStruct;


#ifdef a_h
#define a_h
#include "gstruct.h"
GlobalStruct a_something(...);


#include "gstruct.h"
#include "a.h"
GlobalStruct a_something(...){...}


#ifdef b_h
#define b_h
#include "gstruct.h"
GlobalStruct b_something(...);


#include "gstruct.h"
#include "b.h"
GlobalStruct b_something(...){...}


Is this ok? Because if it is I'm missing something really silly/small/stupid.

BTW, I'm compiling with gcc main.c a.c b.c -o the_thing


I just created an online example so you can see it, download it and try it. It's ready-to-compile but it will fail at compilation time.


share|improve this question
Did you try that sample code you posted? Besides the #include file not being quoted, it looks like it would work... –  Kyle Lacy Aug 23 '12 at 23:01
Er... exactly as you said in the question? (only remember the include guards, the semicolon at the end of the typedef and the quotation marks in the #include) –  Matteo Italia Aug 23 '12 at 23:02
Kerrek touched on how your struct name is reserved. Here's a backup link with more cases: stackoverflow.com/questions/228783/… –  chris Aug 23 '12 at 23:16
There's no #endif in your one header. –  chris Aug 24 '12 at 0:20
That's just a typo, sorry. I do have all #endif 's in my files –  alexandernst Aug 24 '12 at 0:21
add comment

2 Answers

up vote 1 down vote accepted

In the header files, #ifdef should be #ifndef

share|improve this answer
I knew I was missing something really stupid, but I really didn't think about THAT ! Thank you :D –  alexandernst Aug 24 '12 at 1:46
add comment

You're almost there. Three errors need to be fixed:

  1. Add quotation marks to the include directive:

    #include "globalstruct.h"
  2. You need a semicolon at the end of the typedef declaration.

  3. You mustn't use underscore-capital names, as those are reserved. Instead use something like:

    typedef struct GlobalStruct_struct { /* ... */ } GlobalStruct;

(Thanks to @chris for spotting No. 2!)

share|improve this answer
+1; also, include guards may be worth mentioning. –  Matteo Italia Aug 23 '12 at 23:03
What about using GlobalStruct in a header file? Should I include globalstruct.h in the .c or the .h file? Or in both? –  alexandernst Aug 23 '12 at 23:05
@alexandernst: You define the structure in the header file, and you include the header file in all the .c files that need the structure and in all other headers that need its complete type. –  Kerrek SB Aug 23 '12 at 23:09
@alexandernst, You might have missed the semicolon: typedef struct tagStruct {...} Struct; <- –  chris Aug 23 '12 at 23:17
show 10 more comments

Your Answer


