Public Group

# Multiple Source Files

This topic is 4349 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?

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5

• 11
• 20
• 12
• 13
• 14
• ### Forum Statistics

• Total Topics
632146
• Total Posts
3004433

×