1

I need to define a struct, let's say

//globalstruct.h
typedef struct _GlobalStruct {
    int a, b;
} GlobalStruct

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

For example:

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

How can I do this?

Regards

----EDIT----

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

//main.c
#include "gstruct.h"
#include "a.h"
#include "b.h"

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

.

//gstruct.h
#ifdef gstruct_h
#define gstruct_h
typedef struct _GlobalStruct{
    int a, b;
} GlobalStruct;
#endif

.

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

.

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

.

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

.

//b.c
#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

---SECOND EDIT----

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.

https://compilr.com/alexandernst/c-headers

alexandernst
  • 14,352
  • 22
  • 97
  • 197
  • 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: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – chris Aug 23 '12 at 23:16
  • There's no `#endif` in your one header. – chris Aug 24 '12 at 00:20
  • That's just a typo, sorry. I do have all #endif 's in my files – alexandernst Aug 24 '12 at 00:21

2 Answers2

4

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!)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 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
  • Well, I must be doing something wrong because I have the #include "globalstruct.h" in my .h and my .c file (and in my main.c file) and I 'm still getting error: unknown type name "GlobalStruct". What could be wrong? – alexandernst Aug 23 '12 at 23:13
  • 1
    @alexandernst, You might have missed the semicolon: `typedef struct tagStruct {...} Struct; <-` – chris Aug 23 '12 at 23:17
  • @alexandernst, Then I don't know what to say other than check your spelling and make sure the include directive comes before the function. – chris Aug 23 '12 at 23:22
  • @KerrekSB Just to be sure, what do you mean with "include the header file in all the .c files that need the structure and in all other headers that need its complete type." ? Do you mean that I need to use ```GlobalStruct_struct``` in my ```test.h``` ? – alexandernst Aug 23 '12 at 23:27
  • @alexandernst: Maybe. I can't read your mind, but if `test` is declared in `test.h`, then yes. – Kerrek SB Aug 23 '12 at 23:29
  • @alexandernst, Any file thereafter that uses `GlobalStruct` should include the file. When including it more than once, however, you need header guards to prevent the struct from being defined more than once. – chris Aug 23 '12 at 23:29
  • If you have a second header, `other.h`, that declares a function that relies on the details of `GlobalStruct`, then `other.h` should include the line `#include "globalstruct.h"` so that users of `other.h` don't have to worry about what else to include. You need `#ifndef GLOBALSTRUCT_H_INCLUDED / #define GLOBALSTRUCT_H_INCLUDED / ...contents of header... / #endif` in `globalstruct.h` (and a similar construct in `other.h`). If the function declared in `other.h` only needs a `GlobalStruct` pointer, then you could avoid using `globalstruct.h`, though it is a little fiddly to get right. – Jonathan Leffler Aug 23 '12 at 23:36
  • 1
    See [Does the C Standard Consider that there are one or two struct types here?](http://stackoverflow.com/questions/11697705/) and [What are extern variables in C](http://stackoverflow.com/questions/1433204/). – Jonathan Leffler Aug 24 '12 at 00:13
  • So I should use extern instead of my actual implementation? :s I'm getting more and more confused :( – alexandernst Aug 24 '12 at 00:30
2

In the header files, #ifdef should be #ifndef

Alec Danyshchuk
  • 307
  • 2
  • 12