Public Group

# Splitting source up into multiple files

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

## Recommended Posts

How would I go about splitting source up into multiple files? I've made my game (well the first bit) in one single source file, but it's beginning to become cluttered, so how would I go about splitting it up into seperate source files?

##### Share on other sites
This confused me a lot... until my last "project", in which I just hooked everything through stdafx.h and stdafx.cpp. (In other words, every new .cpp I added had #include "stdafx.h" in it and nothing else, all other include files were included through stdafx). Well, it worked fine, but it seemed too easy to be the "right" method. Is there a problem in doing this?

##### Share on other sites
Not a problem, per se, but it adds to your compile time and is not quite the same as what the article in SiCrane's post recommends (I recommend it also). To sum it up:

1. Make a header for every source file that contains functions, variables or classes other source files need
2. Make sure every header has include guards
3. In each source file, include: a) stdafx.h if you like, b) the source file's own header file, and c) other header files AS NECESSARY

This will keep everything structured in an easy-to-access way, and keep your compile time to a minimum.

stdafx.h, MSVC's precompiled header, is intended to be an easy way to include code that all parts of your source use. Candidates for this include the windows headers and headers for other libraries or APIs you may be using (DirectX, Allegro, SDL, etc.)

##### Share on other sites
Quote:
 Original post by benryves Well, it worked fine, but it seemed too easy to be the "right" method.

As far as I'm concerned, there is no "right" way to program. Did your app compile and do what you wanted it to do? Then you did it "right".
As for splitting into multiple source files, do what seems logical. One file should handle graphics init, another input init, one should handle the actually game code (or more than one if you have a really big game, that is usually the case.), etc.

##### Share on other sites
Quote:
Original post by programwizard
Quote:
 Original post by benryves Well, it worked fine, but it seemed too easy to be the "right" method.

As far as I'm concerned, there is no "right" way to program. Did your app compile and do what you wanted it to do? Then you did it "right".

Oh dear god. I'd like to respond to that properly, but I have work to get done today. :S

##### Share on other sites
This is confusing me, probably because it isn't spelling it out.

Say I have a header called vardec.h and I have two source files - main.cpp and refresh_screen.cpp

Now in the top of those two source files there's the include to vardec.h
Now the main.cpp also has the include to allegro.h, and refresh_screen.cpp, i don't know if it stops at the end of refresh_screen.cpp when it errors.

It seems to error because it doesn't have the allegro.h included in it, but that's included in main.cpp, so why does it do that?

These are the three sources:

Main.cpp:
#include <allegro.h>#include "vardec.h"#include "refresh_screen.cpp"void init();void deinit();int main() {	init();	while (!key[KEY_ESC]) {		rest(30);		char buffer[33];		allegro_message( itoa(tool,buffer,10) );	}	deinit();	return 0;}END_OF_MAIN();void init() {	int depth, res;	allegro_init();	depth = desktop_color_depth();	if (depth == 0) depth = 32;	set_color_depth(depth);	res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);	if (res != 0) {		allegro_message(allegro_error);		exit(-1);	}	install_timer();	install_keyboard();	install_mouse();	show_mouse(screen);	/* add other initializations here */}void deinit() {	clear_keybuf();	/* add other deinitializations here */}

refresh_screen.cpp:
#include "vardec.h"void do_alert(){		char buffer[33];		allegro_message( itoa(tool,buffer,10) );     }

vardec.h:
int tool = 1;int zoom = 1;

How can I get that to work (there's nothing wrong with the code, just a problem with the way I'm linking it all together).

##### Share on other sites
When you declare non-const global variables in header files, you need to declare them as extern in the header file, and then in a single source file define them as normal. So your vardec.h file should look like:
extern int tool;extern int zoom;

And in one source file you should have:
int tool = 1;int zoom = 1;

Then also every source file that needs to include a header, needs to do so individually. Your refresh_screen.cpp file uses allegro.h but doesn't include it. To fix that you need to include allegro.h in the referesh_screen.cpp file.

##### Share on other sites
Right I've managed to get that working with ints.
But my problem now is with BITMAPs.

If you don't know allegro you define BITMAPs like this:

BITMAP *image;

Now I've declared them as:

extern BITMAP *image;
extern BITMAP *buffer;

In vardec.h

But now in refresh_screen.cpp it tells me I have multiple definitions of image and buffer.
Which I suppose I do, because I have:

#include "vardec.h"

BITMAP *image, *buffer;

And I've already told you what vardec does, so how do I get around this?

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633417
• Total Posts
3011782
• ### Who's Online (See full list)

There are no registered users currently online

×