Archived

This topic is now archived and is closed to further replies.

TerraX

Multiple source files & structures?

Recommended Posts

Hiya, firstly sorry about the off topic nature of this post, but I dunno where there are other forums related to C which have such talented people My problem is this.... I have multiple source and header files for my latest OpenGL space shooter type game and I want to be able to share structures across these files. I know how to do it with simple variables Example. int AmountOfCheese; // variable in level1.cpp extern int AmountOfCheese; // variable in level1.h I would also put #include "level1.h" at the top of the cpp file. I firstly, am not sure if the above way is correct (teaching myself c at the same time), but it works for me SO, again, my question How do I share structures between multiple files? Please help, it''d make my life alot easier

Share this post


Link to post
Share on other sites
Hi,

Well that''s the way I do it but I taught myself as well

Anyway to share a struct all you have to do is write the structure in a header file and then include the header file with any source files that need to access the structure. Then use extern like you did for the variables. For example, say you wanted to share the structure for a Vertex between files in a header file you could put the following:

struct VERTEX {
float x, y, z;
};

extern VERTEX Vertex1;

Then in the source file:
#include "Header.h"

VERTEX Vertex1;

I hope that helps

Lukerd

"To err is human, to really mess up requires a computer"

Share this post


Link to post
Share on other sites
to share structs among different files you just have to put all structs declarations you need to be seen by the files wich will share the declarations in a simple .h header. Example:

in structs.h:
myStruct {
int x;
int y;
};

in code.c:
#include "structs.h"
myStruct point;

in code2.c:
#include "structs.h"
myStruct *point3;

...and so you will be sharing the declaration of myStruct among the files code.c and code2.c

I hope this help you. Sorry for my english

Share this post


Link to post
Share on other sites
Worked a treat guyz thanx!!!!

While I''m here...
Marcos, your English is MUCH better than any of my other languages, perfeclty readable and understandable

Share this post


Link to post
Share on other sites