Advertisement

really stupid question - from lamoth's "gurus" book

Started by December 23, 2002 12:23 AM
2 comments, last by LuckyNewbie 21 years, 10 months ago
in the first program introduced, there are three files... "breakout.cpp", "blackbox.h" and "blackbox.cpp". both breakout.cpp and blackbox.cpp #include blackbox.h, but neither of these files include the other. (blackbox.h doesn''t either). However, breakout.cpp is free to call functions from blackbox.cpp (that were declared in blackbox.h) Am I missing a core concept behind header/source files here? Someone please help me out (again).
"Let me just ejaculate some ideas"
To call a function in C++, all you need to know is the function signature. The function signature is traditionally declared in a header file (in your case, blackbox.h) and then #included by all source files that need that set of functions.

Remember that #include is esentially a textual replacement of the file #included, so you can get the same results by just copying the declarations in blackbox.h into both blackbox.cpp and breakout.cpp. However, as you should see, this quickly becomes unmanigable, as if you change one function slightly, you have to change EVERY declaration of the function.

That is why header files exist; they localize all the declarations into one file that is easily managable.
Advertisement
Your name is very familiar, are you by any chance LuckyNewbie: P from B.Net?

If not, nevermind...

Edit: Lets see if that works... Nope...
subtract the space between the : and P

[edited by - Zeraan on December 23, 2002 1:57:05 AM]
The idea of separate source files is to avoid having to recompile all your code every time you change a single line. Ideally, your header files would be written once and never need to be changed, while all your bugs are in the source files...

When your project is built, there are three stages. First the preprocessor goes through your source file ''include''ing headers, replacing ''define''d strings, etc. Then your source file (filename.cpp) is compiled to an object file (filename.o), which is pretty close to machine code, but external references remain unresolved - breakout.o will have places where functions from blackbox.o are supposed to be called, where instead there''s just a place-holder. This is repeated for all your source files (or those which have changed or have a dependency on a changed file). Finally the object files are "linked" into a single, machine readable, file (projectname.exe). At this stage, all external dependencies are resolved - place-holders for functions and "extern" variables (and anything else I''ve forgotten) are replaced with the relevant code (in the case of functions, to jump the program execution to the actual function code). Also at this stage, the "main" or "winmain" function will be identified and set as the entry point for your program.

Most "compilers" actually pre-process, compile and link for you. Some treat linking as a separate process. There are a handful of stand-alone pre-processors - primarily used as debugging tools. I''m not aware of any packages that include a stand-alone compiling program

This topic is closed to new replies.

Advertisement