Advertisement

Newbie C++ question

Started by February 02, 2001 02:29 PM
6 comments, last by penetrator 24 years ago
I''d like to have my program divided in multiple .cpp files, like the following example: main.cpp mission.cpp 3dmodels.cpp sound.cpp and then call, from the main.cpp, functions that reside in the other modules. How do I perform that ?
declare

extern vartype varname

in the files you want to use it in...

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

My PGP Signature, Locate Public key on domain server.

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use

iQA/AwUBOcbGNN42vCuvqlVSEQI2mQCglnHz5n1VcNacPQKRtSw8SAFGGWMAn1ei
GyvSAwu1npdATbq3BwdhXIA1
=Vm6v
-----END PGP SIGNATURE-----
Gamedev's AI Auto-Reply bot.
Advertisement
If you only want to use functions (and not variables), all you have to do is to add the header :

      // mission.hvoid Foo(int var);// mission.cpp#include "mission.h"void Foo(int var);{  DoStuff();}// main.cpp#include "mission.h"Foo(4);      


Edited by - MuteAngel on February 2, 2001 3:48:07 PM

Seesh it won't take the returns..

Edited by - MuteAngel on February 2, 2001 3:50:30 PM
Ah that is not true. Here is how you can achieve complete multi-file apps that compile just like 1 file.

//main.h
#ifndef _main_header_included_
#define _main_header_included_

//other includes
#include //same as before

//globals
extern float client_pos[3]; //extern is required or linker goes apeshit

//structs/data types--same as before

//prototypes
void HandleClientPos(float pos[3]); //same as before

#endif //_main_header_included_

Then include this in all files that need the information contained and in main.cpp define all your globals. For example "extern float client_pos[3]" would be written at the top of main as "float client_pos[3];". Then in your other files you can just type client_pos and it will contain the last data entered into it like it was local.

Hunter-Killer
HK, that's not really the best way either, although it works well enough though

Normally the whole point of moving things into different source files is to make your code more modular, i.e. move all 3d stuff to 3d.cpp/3d.h, all input stuff to input.cpp/input.h, etc. so if you have any global variables that are used to represent things in those modules, you put them in the related .cpp file, not in your main source file.

So for the same example already used here...

mission.h
  #ifndef _MISSION_H_#define _MISSION_H_// mission.hvoid Foo(int var);extern float client_pos[3];#endif  


mission.cpp
  // mission.cpp#include "mission.h"void Foo(int var);{  DoStuff();}float client_pos[3];  


main.cpp
  // main.cpp#include "mission.h"Foo(4);client_pos[2] = 45.3;  


Only a small change I know, but it keeps all related code together. If you come back to maintain it, you know that client_pos is related to your mission file, so that's where to expect it. And if your project gets big, your main.cpp isn't mostly full of global variable definitions.

This is all assuming that client_pos is meant to be grouped with mission.cpp though. But generally speaking any variables that need to be global should be in a seperate header file. This is because global variables are normally looked on as 'bad' practise (it doesn't mean that you can't/shouldn't use them though, if the circumstances require it). Keeping them in a seperate header means that it reduces the variables scope to just the source files that need access to it (by #including the header), not blindly making it accessible to every part of your program.

If you want to know why you have to put these things in a header files, rather than just how, I'll quickly explain.
Your compiler compiles each .cpp file to an object file, and these object files normally know _only_ about variables and functions available inside them. Then all of the object files get linked into the final .exe file, which brings all of the functions and variables together. By including a header file which declares that some extra functions and variables exist outside of the current source file, you are basically saying 'In addition to anything I've written here, I am telling you that these extra functions/variables exist somewhere else, you will be able to access them after you have been linked'.

Anyway, I hope that gave a little bit more (than needed...) info to penetrator, and anyone else interested.

Dan

Edited by - danbrown on February 5, 2001 2:13:00 PM

Dan

Apart from keeping code nice and easy to handle, using multiple include files cuts down the compile time. If you have a single global main.h file, then everytime you modified it your entire project would have to be recompiled and linked because all your .cpp files rely on it.
If you have multiple .h files, then only the files that depend on the header file you modify need to be compiled.
This probably won''t seem much to you if you only have a small project with a few files, but as the project gets larger, the compile time significantly increases.
Gee Brain, what we gonna do tonight?
Advertisement
please please use classes. OOP is the way to go!
my file structure is pretty simple

one main header which includes all other headers

--main.h--
#include "input.h"
#include "sound.h"
etc

and at the top of all of my cpp files i just stick #include "main.h"


http://members.xoom.com/myBollux

This topic is closed to new replies.

Advertisement