🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

A newbie question

Started by
7 comments, last by DarkMage139 24 years, 4 months ago
Multiple source files... what''s the deal with them. I never really wondered about them until recently. I pretty much kept all the code in lots of INCLUDE files, and my games only had one source file. Then I encountered a game with multiple source files. I tried putting functions in different ".cpp" files with unsuccessful results. How do you do that?! - DarkMage139
- DarkMage139
Advertisement
Manuals are great. Same for online documentation.
William Reiach - Human Extrodinaire

Marlene and Me


Gromit, have u ever responded nicely to a post ?

Here''s how it works.

You have a header file definig the declarations of stuff say

////////////////////////
clas MyClass
{
stuff here
};
///////////////////////

Then you have a cpp file that defines ur stuff.

//////////////////////////////////
#include "myclass.h"
void MyClass::Stuff( other stuff )
{
}
///////////////////////////////////

So basicly you can include ur headers in any file that needs it say a header like:

///////////////////////////
#include "myclass.h"
class BigClass
{
has a MyClass
}
//////////////////////////

and u don''t get compiler errors cuz nothing is being redefined, It''s seperated into logical easy to find sections, so it''s easier to keep track of, and easier to modify. Simple huh?

Dare To Think Outside The Box
_____________________________
|____________________________|
http://www.inversestudios.com
Write more poetry.http://www.Me-Zine.org
Almost all of the time.

You forgot that you have to add the .cpp file to your project.
William Reiach - Human Extrodinaire

Marlene and Me


Gromits'' my idol on this message board so don''t be to offended when I say...Read a damn book!! they are usefull..
BUT, since I am in a fairly good mood since I have figured out a problem thats been buggin me for hours on end I will shed some light on the subject....
A couple of the main reasons for using different source files is to:
1) Make for more orginized and easier to read(theroeticaly) code.
2) Also good for hiding implementation...say you want someone to beable to see your header file so they know whats all in it but you don''t want them to have access to the source for the functions themselves so you would put that in a seperate .cpp file.

Later
OME
*blush*

They are also good for modular programming. Easier to port to another machine.
William Reiach - Human Extrodinaire

Marlene and Me


I also here multiple cpp files can speed up re-compiles. By this I mean if you compile and then decide you need to make a change in one file (not a rare case that the second compile will be faster. Is this so? I assume its cuz object files are made for each cpp file....
A separate .obj file is made for each .cpp files, so changing only one file means that the compiler only needs to update that .obj file and relink. However if you change a header file, all the .cpp files that use that header have to be re-compiled.

One thing, though, the multiple source file question has been asked on this board before. You might want to look those posts up for more info.
Yeah, thanks, everyone!

- DarkMage139
- DarkMage139

This topic is closed to new replies.

Advertisement