# Multiple Source Files

This topic is 4175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. My program is getting quite large and I'd like to split it up into smaller .cpp files but I don't know how to. I've made some attempts but they don't work as I've basically guessed what I need to do. I can find guides telling me about how wonderful it is but not what I actually need to do! If someone could provide me with a guide or some examples that would be great. Basically what do I need to put in the headers, what do I do about global variables (YES I need them) defining functions and so on. Oh and I don't know how to use classes, so it's no good talking about them :P (Well I've come along just fine so far without them) THANKS!!!

##### Share on other sites
Take a look here.

##### Share on other sites
Yes I have already read that. It does not tell me what I need to do. It assumes I know how to do it already, thus defeating the point of the guide.

##### Share on other sites
Are you sure you didn't just read the first page? It's five pages long and explains what to put in header files, what to do about gobal variables, where to define functions, and so on. If you did read all of it perhaps it would be easier for you to quote some part you found to be vague and we can help clear it up.

##### Share on other sites
Here is the all-on-one-HTML-page version.

Quote:

I don't do this kind of summary every day. Maybe I should, though. :s

##### Share on other sites
Thanks but it's all gobbledegook to me.

Consider this simple example (I'm not at my compiler so I don't know if it has any errors in it)

start of main.cpp file
int global = 5;
void show(int var);

int main()
{
show(global);
return 0;
}

void show(int var)
{
if(global == var)
{
cout << "yay";
}
return;
}

how could I split the show function onto another file?

##### Share on other sites
Quote:
 how could I split the show function onto another file?

show.h
void show(int var);

show.cpp
#include "show.h"void show(int var){if(global == var){cout << "yay";}return;}

main.cpp
#include "show.h"int global = 5;int main(){show(global);return 0;}

##### Share on other sites
While COleException is quite correct, I just thought you might like to see how to put the global int in show.h/.cpp as well. Header guards are a good idea in show.h as well to prevent it being included twice:

show.h
#ifndef SHOW_H#define SHOW_Hextern int global; // no initialiser herevoid show(int var);#endif

show.cpp
#include "show.h"#include <iostream>int global=129213; // initialiser herevoid show(int var){    if(global == var)        {        std::cout << "yay" << std::endl;        }    return;}

main.cpp
#include "show.h"int main(){    show(global);    return 0;}

How you then compile and link these two files is dependant on your compiler and IDE. In VSEE, just having them both in the same project does the business. With a command line compiler, you'd probably do something like:

bcc32 main.cpp show.cpp

Unless you were using make or something, then your makefile might look like this:

main.exe : main.obj show.obj    bcc32 main.obj show.objmain.obj : main.cpp show.h    bcc32 -c main.cppshow.obj : show.cpp show.h    bcc32 -c show.cpp

##### Share on other sites
It appears to be working but I have an enumeration that does not seem to work. Where should I define the enumeration?

enum e_pieces{q=-7, r=-6, b=-5, k=-3, n=-2, p=-1, empty=0, P=1, N=2, K=3, B=5, R=6, Q=7};

e_pieces board[128];

Where should I put this? board needs to be a global variable. It says board indentifer not found even with argument dependant lookup. It thinks it is a function? extern e_pieces board[128]; results in missing ; before board. And I get tons of K undeclared identifier etc and so on. Any advice?

I am using Visual Studio .NET 2003.

##### Share on other sites
Put the enumeration in a header file. As long as the header is not included twice, which include guards will prevent, it will be fine.

To declare a global array like that, do this:

file.h
extern e_pieces board[]; // no size or initialisers specified here

file.cpp
e_pieces board[128]={ q,r,b }; // all as normal here

Looks like a chess program then?

##### Share on other sites
What gave it away ;P

##### Share on other sites
It still doesn't work.

OK I have "Angel Chess.cpp" which until now contained everything. Now I have created Angel Chess.h boardsetup.cpp and boardsetup.h.

At the moment on the top of angel chess.cpp I have this

#include "stdafx.h"
#include "iostream"
#include "math.h"
#include "stdlib.h"
#include "Angel Chess.h"
#include "boardsetup.h"
using namespace std;

(ok maybe a bit overboard but I wasn't sure which ones I'd need, and it works fine otherwise.)

In boardsetup.h I have same variables extern-ed which work fine and a function entry.

I've tried putting the enum definition and the board everwhere it seems and I just can't get it to work. It keeps saying they are undeclared identifiers. AArgh!

##### Share on other sites
Post the complete code for Angel Chess.h, boardsetup.h, boardsetup.cpp and main.cpp (in [ source ] tags) and I'm sure someone will be able to tell you where you are going wrong. The structure for the enum and global array I posted is correct for any standard C++ compiler so, hopefully without sounding too arrogant, I am sure the problem is something simple.

Are you sure your compiler supports filenames with spaces in, by the way?

Also, as an aside, you should really put your standard library includes in <> rather than "" and just your local project includes in "". It affects the order the compiler uses to search for files.

#include <iostream>

Here's a minimal example in the meantime. Perhaps you could have a look and see how your code differs:

chess.h
#ifndef CHESS_H#define CHESS_Henum pieces { rook,knight,queen };extern pieces board[];#endif

chess.cpp
#include "chess.h" // you've definately done this, yes? The following code needs                   // to be able to see the enum definition as well as any units                   // that use chess.hpieces board[64]={ rook,knight }; // etc

main.cpp
#include "chess.h"int main(){    board[0]=queen;    return 0;}

One thing just occured to me - are you getting compiler errors or linker errors? If the former, you have your declarations in the wrong place or something like that. If the latter, you are not linking the resultant object files together correctly.

HTH Paul

##### Share on other sites
That is not a good idea, as the whole problem is my code is too long to manage!

But I've managed to get it working somehow. I simply defined e_pieces in every header file, and used the #ifndef trick to prevent redefinition, and it now seems to be working.

##### Share on other sites
OK now something very strange has happened. In boardsetup.cpp I get all my couts endls etc saying undeclared identifier.

boardsetup.h
#ifndef BOARDSETUP_H#define BOARDSETUP_Hint setupboard(char input[]);int cellconvert(int cell);int drawboard();//extern e_pieces deadlist[2][16];extern int deadnum[2];//aiextern int aimoves[2][1][2]; //player, moves aheadextern int playertype[2];extern int tomove, whitecastleking, whitecastlequeen, blackcastleking, blackcastlequeen, moverule, turn;extern int moveslist[2][250][2];extern int movesnum[2];extern int capturesnum[2];extern int captureslist[2][250][2];extern int piecenum[2];extern int piece[2][16];extern const int white;extern const int black;extern const int black2;extern const int xnum;#ifndef EPIECE#define EPIECEenum e_pieces{q=-7, r=-6, b=-5, k=-3, n=-2, p=-1, empty=0, P=1, N=2, K=3, B=5, R=6, Q=7};#endifextern e_pieces board[];int cellconvert(int cell);#endif

boardsetup.cpp (only problem function shown)
#include <stdafx.h>#include <iostream>#include <math.h>#include <stdlib.h>#include "boardsetup.h"int drawboard(){	int a = -1;	int d = 8;	//char c = 'A';	bool cellcheck = false;	for(int x = 0; x < 120; x++)	{		a++;		if((a) % xnum==0)		{			if(a != 0)			{			cout << d--;			}			cout << endl;			cout << "------------------------";			cout << endl;		}		cout << '|';		while(cellcheck == false && x < 128)		{			int b = x & 0x88;			if(b == 0)			{				cellcheck = true;			}			else			{				x++;			}		}		cellcheck = false;	//	cout << a;		switch(board[x])		{			case empty:				cout << ' ';				break;			case K:				cout << 'K';				break;			case Q:				cout << 'Q';				break;			case R:				cout << 'R';				break;			case B:				cout << 'B';				break;			case N:				cout << 'N';				break;			case P:				cout << 'P';				break;			case k:				cout << 'k';				break;			case q:				cout << 'q';				break;			case r:				cout << 'r';				break;			case b:				cout << 'b';				break;			case n:				cout << 'n';				break;			case p:				cout << 'p';				break;		}		cout << '|';	}	cout << d--;	cout << endl << "------------------------" << endl;	for(char c = 'A'; c < 'I'; c++)	{		cout << ' ' << c << ' ';	}	return 0;}

##### Share on other sites
Fixed it - forgot using namespace std;

##### Share on other sites
I'm confused. Where you have:

where are you actually doing:

##### Share on other sites
In Angel Chess.cpp, which I have not included (partly because it's 50 pages long ^_^)