Jump to content
  • Advertisement
Sign in to follow this  
grekster

Multiple Files (.cpp & .h ....)

This topic is 5455 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

I was messing about with having multiple files in one project today because although I read how to do it on a few different articles, it didnt seem like it would work and so I tried it out. Anyway i created a main.cpp with my int main in, and then i created a math.cpp with a TimesTwo function and a math.h file with the function decleration for TimesTwo. I included the math.h header file in my main.cpp, compiled and ran and low and behold it worked, just like all the articles said it would. WHY!!!!!!! Theres nothing in the math.h saying which file the function definitions are and theres nothing in math.cpp saying where the declerations are. so how does the compiler know where to look? my first thought was that it must just search through all the files included in the project(I was using VisualC++ so by project I mean a VC++ project) but wouldnt this be very slow for a big project? Wouldnt it be better to have a link to the .cpp file in the header so the compiler knew where to get the function definitions? Also what about in compilers where a project isnt made before hand (like in VC++) what does the compiler do to find the function definitions then? This is really confusing me id be grateful if someone could give a bit of an explaination :) (Think thats one of the longest posts ive ever made on gamedev lol)

Share this post


Link to post
Share on other sites
Advertisement
VC++ automatically linked with the object file generated from math.cpp(probably called math.o or math.obj)

Share this post


Link to post
Share on other sites
Yeah but that doesnt really help, how does VC++ know which object files to link? I know that it automatically linked the right files together cause the program worked. I was just wondering how it knew which file(s) to use:S

Share this post


Link to post
Share on other sites
well, the files are stored in the project file, it simply links all the object files that are generated from the cpp files that are in the project.

Share this post


Link to post
Share on other sites
Simple. What happens when you include a header file is simple copy/paste. To the compiler it looks like the header file is just pasted into the beginning of the cpp file.

Every cpp file is compiled. Not to a final executable, but to a temporary object file.

The thing is, each cpp file is compiled independently, with no knowledge of the other files in the project. Thats why you need to include headers for functions you use, but don't define. You're basically telling the compiler "I'm using the TimesTwo function, which takes these arguments, and returns this. And don't worry, it'll be defined somewhere else, so quit whining". The point is that all you need to call a function, is the argument and return types, and the address of the function. You dont need to know what the function actually does.
The address can't be provided yet, but is easy to fill in later. The others can be supplied just by looking at the prototype in the header file.

Then in the object files that result from compilation, you'll have labels saying something like "insert call to the TimesTwo function here, as soon as we've actually found the TimesTwo function".

Once all the cpp files have been compiled to these object files, the linker is called, and yes, it is told which files to look through, although this happens automatically in VC++. It's just handed a list of all the object files belonging to the project (If you didn't have a handy IDE to do this for you, you'd just call it with all the files as arguments), and it then merges them into one executable. It'll basically just paste the code from all the files into one big executable, determine the address of the the TimesTwo function, and then insert that in all the calls to the function.

The trick is that the functions are all compiled separately, with no knowledge of other functions, or the rest of your project. So no, the compiler doesn't know where your functions are defined when calling them. And it doesn't need to know. It just needs to know that "it's not my problem. I know the function's interface, and I know the linker will complain if it isn't defined somewhere else".
When they're compiled, the linker is handed *all* the files, and, well, links them together. It just has to check that every function is defined once, and then insert the correct addresses every time you call a function. This has to be done sooner or later anyway, and doesn't take too much time, even with large projects.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
best way to get a feel for this and to understand things is to use a command line compiler for a while

Share this post


Link to post
Share on other sites
That was a great explanation thanks! I understand now (no more screaming at the compiler because it can find the files lol)

Share this post


Link to post
Share on other sites
This is an analogy I use with my C/C++ students. Take your own conclussions.

a. Consider each CPP file like a 'black box'. Each box is totally and completely closed and independent of any other cpp file. Each one is a completely enclosed environment. So, as it is closed, a 'black box' cannot know the contents (functions) inside other 'black box' and other 'black boxes' cannot know the contents of this cpp file. Considering that your void main(void) function is also inside a cpp, that cpp is also a black box.
In you program, the main.cpp file and math.cpp file are black boxes and dont know each other contents.

b. Even when each box is closed you can expose a black box content and thats the reason of header(.h) files. Think about them like an item catalog in a warehouse. You put all the boxes inside the warehouse and keep a catalog of the items inside each box. Each page in you catalog has the same name than the box where the items you keep track of are located, but thats just just for order this is not required. You write in each page (.h) the items you want to remember that are inside the box (int sin(angle); int cos(angle);). In this case math.h is the catalog for math.cpp. Just remember, a header file exposes the items you want from each box, but you can keep some items secret.

c. If you want other box to be able to REFERENCE items inside one of your boxes, you tell it to include a header file. Thats the reason you #include "math.h" in your main program file. Once you include a file, you allow that cpp to use the functions in that header. Notice that you aren't including the function bodies (the other CPP file), you are just including the reference. C is a very nice language and always consider that you know what you do. So, when you include a function reference it considers it can USE the function. Doesn't matter if it is not at hand at the moment... it considers you will provide it later.

Now consider this, without the .h files you have a bunch of boxes that cannot work together because thay cannot see each other. But now that you add header files, they can reference each other. So header files are the glue that join your boxes.

d. When you create a proyect, you only are required to add the cpp files to your proyect, h files are not required as they should be already #included in your files, thats why they are not required. But they are really important as ther glue your cpp files together.

e. During the compilation process, each CPP file is compiled separately. If they use a function not defined inside that CPP, the function is searched in the #include files. If the function is included there then its ok. During compile time the body of each function is NOT required, the only thing required is that functions are declared and accessible to each CPP. That's why you can do #include <stdio.h> even when you dont have the code or the functions located in stdio.lib (go ahead, look lib files in your PC)

f. The final step is the link. During the link all boxes are glued toghether in a single EXE file. The EXE file contains all the cpp files included in the project AND any other obj or lib used by the project. In this final step function bodies are important. If there is any function declared in a .h file that has no body, then the linker will display an error.


And thats it, cant explain more as this is just a forum. Also I'm no native english speaker. This is the base for modular programming and once you get it you have the base for any advanced work.

Luck!
Guimo




Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!