I'm wondering how does the complier link files together? Is it linked once you run all files in 1 single program or do I need to specify somewhere? |
Normally all source files (.cpp) in your project is compiled and then linked together to produce the executable file. There is something called
dynamic linking which is quite common with libraries which means the library will be loaded and linked when you run the program. This has a few advantages, it reduces the size of your program and it allows the user to upgrade the library without recompiling your program, but this also means the library has to be installed in order to run your program, and you have less control over which version of the library is being used.
If you use an IDE much of this is hidden from you but there are many compiler settings you can use to control how compilation and linking is done.
And when do I need a header? If the files are linked when complied, why do I still need a header? I mean the complier can cross reference by itself? Like for example if you use "main.cpp" and "test.cpp". |
A header is never strictly needed, it just makes it easier to write your program. #include works as an automatic copy-paste. It simply takes the content of the file and includes it in the file. You would get the exact same result if you replaced the #include with the text in that header file.
If you want to call a function or create an instance of a class the compiler need to know that there is such a function and class, otherwise it will give an error.
1 2 3 4 5
|
int main()
{
A a; // This gives an error because the compiler doesn't know what A is.
foo(a); // This gives an error because the compiler doesn't know what foo is.
}
|
To make it compile we can declare the function and define the class before we use them.
1 2 3 4 5 6 7 8
|
class A {};
void foo(A);
int main()
{
A a;
foo(a);
}
|
This will compile and you could do it like this, but if A and foo will be used elsewhere you would have to add the class definition and the function declaration to all files that use them and that can be error prone. If you made a change to the class you would have to remember to make the exact same change everwhere and it could be a lot of work and easy to make a mistake.
A much better way is to put these things into a header file. Then we only need to include the file when we want to use them and we only have to modify one file if we want to make a change.
A.h
1 2
|
class A {};
void foo(A);
|
main.cpp
1 2 3 4 5 6 7
|
#include "A.h"
int main()
{
A a;
foo(a);
}
|
Note that this has nothing to do with linking. It's just used to make the program compile.
EDIT: Thought I would be more explicit. Say I have this namespace created in test.cpp |
By convention we don't include source files (.cpp). You would have a problem if you tried to include test.cpp from more than one source file because z, test_ns::x and test_ns::y would be defined more than once which is not allowed. You probably be able to compile the files but when everything is linked together it would notice that the variables has been defined more than once and give you a linker error.