Very confused about headers and source files!

I'm working on a project with several thousand lines of code, which I have split up into header files (.h), which look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
//myfile.h
#ifndef _MYFILE_H
#define _MYFILE_H
using namespace std;

#include <....> // include libraries

#include"..." // include my own headers that are needed here

struct somestruct {
//...
};
#endif 


then I have some .cpp files where I've dumped all the functions inside the structures, something like this:

1
2
3
4
5
6
7
8
9
10
11
//myfile.cpp
#ifndef _MYFILE_CPP
#define _MYFILE_CPP
using namespace std;

#include "myfile.cpp" // include the header file

void somestruct::memberfunction() {
// some stuff in here
}
#endif 


But I don't know what I'm supposed to do now, the literature says that I should include the header file in main.cpp, but the header file only calls other header files, not the source file, so how will the compiler ever know where to get the source (.cpp) file from?

Also, should I be declaring the namespace std in every file? I'm totally confused about the way multiple files are treated, and it'd be great if someone could explain to me which #include statements should be included in which files, where the namespace statements should be, and how I tell the compiler where to pull all the .cpp files from (and in which order...).
#include is C++-speak for "copy paste". When the compiler (or an earlier phase of the compiling process, I wouldn't know) encounters an #include statement, it finds the file and copy pastes its content into the calling file.

One thing to keep in mind is to never include .cpp files. If you spread stuff over several files, the header (.h) file will contain pre-declarations. The cpp file will include the header file and define the pre-declared functions. Example:

1
2
3
4
5
6
7
8
//test.h
void myFunction(int i);
int yourFunction(double j);

//test.cpp
#include "test.h"
void myFunction(int i) { // body }
int yourFunction(double j) { // body } 

Now, any file that #includes the header file will be able to use the functions declared in the header, even though the header technically doesn't "explain" the function.
Last edited on
so how will the compiler ever know where to get the source (.cpp) file from?


If you're compiling it, why does it need to know where it is? All C++ code that ever gets compiled is in various *.cpp files. If you use an IDE, this is automatic, and if you use command line, obviously you're giving it the file names...
When the compiler (or an earlier phase of the compiling process, I wouldn't know) encounters an #include statement, it finds the file and copy pastes its content into the calling file.


It's done by the pre-processor, which runs before the actual compiling begins. Most compilers offer the option to stop at this point, so you can see what the code looks like after pre-processing; sometimes a big help to find bugs introduced by #define macros and the like.
Last edited on
I don't understand why you would want to split each header into a .cpp and a .h all I ever use are .h files with the functions inside of it, why would I want to further segment my program files?
Headers should be for declarations only - no actual definitions except for inline functions and templates.
Source files should be used for ther actual definitions - multiple headers can be included in each of multiple source files.

If you defined functions in a header, then included it in two source files, you would get multiple definitions errors from your linker.

The reason to split things into more than one source file is for convenience - surely you do not want to scroll through a single file with 10,000 lines of code? I'd prefer 10 files with 1,000 lines of code, each file having a name related to the code it contains so I know which is which and where to look.
Thanks!
I don't understand why you would want to split each header into a .cpp and a .h all I ever use are .h files with the functions inside of it, why would I want to further segment my program files?


Additionally, if your code is all in one file, you have to recompile the whole thing every time you make a change. On big projects compilation of all code can take hours. Nobody will be impressed if a one line change in a single function means waiting a day for the compilation.
The quick and easy guide to understanding headers, includes, and source files:
http://www.cplusplus.com/forum/general/13162/#msg63354
If you're compiling it, why does it need to know where it is? All C++ code that ever gets compiled is in various *.cpp files. If you use an IDE, this is automatic, and if you use command line, obviously you're giving it the file names...


I think this is the critical piece of info I was missing, that wasn't really explained in the tutorials I read. So if I use a high-level compiler/IDE like Eclipse (in my case), would I expect it to automatically include any .cpp files that it encounters inside my project? Or only those with corresponding header files? Or is it something you would define manually?
It doesn't "include" the *.cpp files, it actually COMPILES them. Where did you think they go included to? #include performs a copy paste operation, it copy-pastes the contents of whatever file you tell it to include - it's not really any special command.
Usually it's a obscure option, something like "adding" files to a "project". Then it will pass these files to the compiler.
Topic archived. No new replies allowed.