Help with linking my c++ files

Hi i need help with putting my c++ files together so i can organize things instead of trying to search through several hundred lines of code when i want to change something. I have a text based game i am reworking to run smoother hopefully but i want it to be more organized as you can imagine having to write out every word in the game takes up space. As well as checking for everything that happens. So how would i throw my functions into different files and have them be able to run via my main function? I have been searching the web for answers but i cant find anything direct that will tell me how. Or it is just plain wrong and doesnt work. Any help would be greatly appreciated =)
My suggestion would be to utilize multiple C++ header and source files; this task is particularly easier if you're using an IDE that allows you to create projects which will make compilation easier.

Let's say you have a bunch of ally and enemy functions in your main file. You can separate these functions definitions neatly into their own allies.cpp and enemies.cpp source files respectively. You then create their respective header files, allies.h and enemies.h, which holds their function declarations. You can then #include the header files in main and enjoy the same access as you had having all the functions in one file. It's the same idea as #include <iostream> expect you are making your own headers instead of using system headers. Your project would now look like this:

1
2
3
4
5
6
7
8
9
//main.cpp
#include "allies.h"
#include "enemies.h"

int main (void)
{
    ally_function();
    enemy_function();
}


1
2
3
4
5
6
7
8
//allies.h - contains allies.cpp function declarations
#ifndef ALLIES
#define ALLIES

void ally_function();
//Declared here. Defined in allies.cpp.

#endif 


1
2
3
4
5
6
7
8
//allies.cpp
#include "allies.h"
#include <string>

void ally_function ()
{
    std::cout << "Hi! I'm an ally!" << std::endl;
}


1
2
3
4
5
6
7
//enemies.h - contains enemies.cpp function declarations
#ifndef ENEMY
#define ENEMY

void enemy_function();
//Declared here. Defined in enemies.cpp.
#endif 


1
2
3
4
5
6
7
8
//enemies.cpp
#include "enemies.h"
#include <string>

void enemy_function()
{
    std::cout << "Grr! I'm an enemy!" << std::endl;
}


Now a bit of a breakdown.
1. Limit header files to declarations. Use CPP files for definitions. This makes your code more portable and easier to understand.

2. Make sure to comment your declarations liberally. Limits the need to go back and scamper through the source code just to know what a function does.

3. Take note of:
1
2
3
4
#ifndef THIS
#define THIS
//declarations here
#endif 

This is known as a header guard, and it prevents your header files from being unnecessarily duplicated during compilation. The compiler checks to see if "THIS" is not defined; the only time it isn't is when it hasn't been included yet. If it has been defined, then the compiler skips to the #endif life, effectively preventing itself from recopying declarations which were already declared.

4. Notice how I had to #include "allies.h" in both main.cpp and allies.cpp. This is because you must include your header file in every individual CPP file you want to access those declarations. Same goes for the #include "enemies.h" for the main.cpp and enemies.cpp source files.

5. You've been using this syntax to include files so far: #include <iostream> . You must use double quotes when including non-standard files but inequalities when including standard files (cstring, iostream, etc...).

6. Try to make your header/source file pairs as autonomous as possible. Don't make them codependent on each other (i.e. both requiring a data type defined in the other at the same time). This may lead to situations where a type isn't defined by the time it is needed, creating a compilation error.

That's all I got for now. :P
I am also attempting to learn how to implement multiple .cpp and .h files in a similar situation. My problem is that I have global int variables for character stats such as Health, Max Health, Gold ect. How do I go about ensuring these variables are accessible through all .cpp files?
I am also attempting to learn how to implement multiple .cpp and .h files in a similar situation. My problem is that I have global int variables for character stats such as Health, Max Health, Gold ect. How do I go about ensuring these variables are accessible through all .cpp files?


You would declare your variable as you typically do in main and then use the keyword extern when you want to access it in a different .cpp file. Your code would look like such:

1
2
3
4
5
6
7
//main.cpp
int globalHealth = 0;

int main (void)
{
    globalHealth = 1;
}


1
2
3
4
5
6
7
8
9
//health_funcs.cpp
#include "health_funcs.h"

extern int globalHealth;

void giveHealth ()
{
    globalHealth += 1;
}


Make sure to not initialize an external variable! It will throw an error if you try extern int globalHealth = 0;.

You could ease the tedious task of using global variables by declaring them all in a global.cpp file and including all the external declarations in a header file.

If I may chime in about global variables, however. They can convolute your code; your code would probably benefit significantly if you limited the scope of your variables as much as possible and pass them by reference to functions that modify them.
I hear that quite often concerning global variables
Edit: thanks for the info above btw
Last edited on
Very very helpful thank you. Its much appreciated. I clearly understand how to do it now thanks to the very detailed description =). Again thank you.
I tried everything you did and it didnt work =/ This is my code:
main.cpp:
1
2
3
4
5
6
7
8
#include <iostream>
#include "myFunc.h"

using namespace std;

int main(){
    myFunction();
}

myFunc.cpp:
1
2
3
4
5
6
7
8
#include <iostream>
#include "myFunc.h"

using namespace std;

int myFunction(){
cout << "Ran myFunction" << endl;
}

myFunc.h:
1
2
3
4
5
#ifndef MYFUNC
#define MYFUNC
int myFunction();
#endif // MYFUNC
Last edited on
int myFunction(); doesn't return a value.
Last edited on
Is it required that I return a value?
If you declare a function has a return type other than void, it must return a value of that type. C++ can be a very picky language at times. :P
That's weird haha. I have more files that I run and never return anything. Oh well. Thanks for the help =). Would there be anything else that could possibly go wrong?
Topic archived. No new replies allowed.