Advertisement Jump to content
Sign in to follow this  

Organising C source files.

This topic is 3100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, none of the books I have nor an hour spent googling have found a satisfactory answer to this - I hope someone here can help!

Let's say I have a program consisting of a single large source file but I wish to split the project into smaller files each containing related functions. Using a game as an example I might want something like:


where each file has access to global variables defined in.... ?

The question is, how do I make the large number of neccesary global variables available to all source files?

I've seen programs that declare variables in header files (I'm told that is bad) and I've seen a setup where the main .c file declares the variables and then a header re-declares them as extern. Painful duplication!

So... can someone please put me out of my misery and explain the correct way to do this?

Many thanks.

Share this post

Link to post
Share on other sites
Maybe you already know this: Organizing C/C++ source files.

Anyway, the extern thing is basically the only way to do globals. The duplication really shouldn't be too bad. It's no more than the name duplication you'd already have to do for function names declared in .h's and implemented in .c's.

Share this post

Link to post
Share on other sites
First of all, you want to minimise the "large number of necessary global variables". Go through each one and ask yourself is it really necessary for this to be global? Could it be passed as a parameter instead?

Next, unfortunately you're going to have put up with the "painful duplication". Declaring variables as extern in a header and defining them in the appropriate .c file is the correct C way of doing this.

By the way, there are varying degrees of "globalness". You may have some variables that are truly global, (although again, is it really necessary for this to be global?) but even if a variable is in the global scope, it should still be declared in the correct header.

So expanding from your example, let's say that for some reason the game title has to be global. You could declare

extern char* GameTitle;

in intro.h and define
char* GameTitle = "My Super Game";
in intro.c

That way only files that need to use GameTitle can include intro.h and anything that doesn't need to know about it can remain blissfully ignorant.

OT: unless you're working on a severely resource constrained environment, do yourself a favour and ditch C.

Share this post

Link to post
Share on other sites

Thanks CE - a great explanation, very helpful indeed.

Apologies for not thanking you sooner - Other problems at this end.

All the best!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!