Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / organizing source files

Author
Message
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 26th Sep 2008 01:35 Edited at: 26th Sep 2008 01:37
In my experiences as a programmer, I've tried to develop good management habits as I arrange my programs into multiple files.

Yet I keep getting stuck on this. I'll have a few files:
main.h:

main.cpp:

init.h:

init.cpp:

output.h:

output.cpp:

The above example obviously has issues.

What would be the best way to fix it?

How would I stick some of my methods in external files, whilst avoiding #include'ing my main header for globals that they depend on?

I know I could pass these globals as parameters from my main source file, but that doesn't always work out the best way.

~you can call me lantz~
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 26th Sep 2008 02:06 Edited at: 26th Sep 2008 02:08
put all of the function declarations in one file, example, externs.h. Then add the #include <externs.h> header once for each .cpp file, and something similar for the variables
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 26th Sep 2008 04:11
could i get a second opinion?

~you can call me lantz~
Gasilli
16
Years of Service
User Offline
Joined: 20th Sep 2008
Location: Having a party in your garage
Posted: 26th Sep 2008 04:15
What I do, is make a file with includes of everything, such as main.h, add all the files and for organising the actualy files I use a folder called components then have folders for each thing they do, for example: world, muliplayer, player, etc. Then I do a nameofcomponent.filename.cpp, keeps it quite organised.
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 26th Sep 2008 04:28
@Gasilli
Could I see an example? a screenshot or some other sort of visual?

@Everyone Else
More techniques and opinions are appreciated.

~you can call me lantz~
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 26th Sep 2008 05:05
I don't recommend an "everything in one" include that is included in every file. That greatly lengthens compile time if you've modified that header (or just about anything related to it).

First of all, use defines for safety (don't remember the name for this technique ):

Example


Do that at the beginning of every header file. That way, nothing is included twice.

After that, it's just a matter of thinking through what order to include. Don't include everything in one header: it's just bad practice. Also avoid globals at all cost.

Rearrange your files like so:

Main.h:


Init.h:


Output.h:


Init.cpp:


Output.cpp:


Main.cpp


Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 26th Sep 2008 06:24
This is all looking very good. I'm curious to see more methods.

~you can call me lantz~
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 26th Sep 2008 06:47
Really, it's a matter of thinking "How can I include as little as possible while keeping it neat?" For example: I removed using namespace std, and typed std::cout instead. That way, you don't have to worry about conflicts, and it's clear when you're using something from the standard library. I also moved the #include <iostream> from output.h to output.cpp, so that it's not included anywhere except output.cpp (the only place it's needed).

Things like that really help.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo

Login to post a reply

Server time is: 2024-09-30 07:14:50
Your offset time is: 2024-09-30 07:14:50