I have recently become aware that I have no idea, genericly speaking, how a c/c++ compiler works. I will admit this initialy came from trying to understand header guards but came to the realization that I am lacking in how compiling works.
Take Visual C++ for instance; Theres the "Header Files" folder, the "Resources Files" folder, and "Source Files" folder. Is there any significance to the separation of these folders and what you put in them? To me, they are all source files. Take the code snippets:
Snippet 1
//a1.h
int r=4;
and
//a1.cpp
int b //<--semicolon left out on purpose
and
//main.cpp
#include <iostream>
#include "a1.h"
void main()
{
cout << r;
}
The compiler errors out saying "a1.cpp(3) : fatal error C1004: unexpected end-of-file found" where I would expect it wouldn't because the a1.cpp file is not #included where the main method exists where in the next code snippet
Snippet 2
//a1.h
int r=4 //<--semicolon left out on purpose
and
//a1.cpp
int b = 4;
and
//main.cpp
#include <iostream>
void main()
{
cout << b;
}
Errors out because "main.cpp(6) : error C2065: 'b' : undeclared identifier". If you include the a1.cpp like so
Snippet 3
//a1.h
int r=4 //<--semicolon left out on purpose
and
//a1.cpp
int b = 4;
and
//main.cpp
#include <iostream>
#include "a1.cpp"
void main()
{
cout << b;
}
the compiler complains "a1.obj : error LNK2005: "int b" (?b@@3HA) already defined in main.obj". Both snippets 2 and 3 ignore the fact that int r = 4
does not have a semicolon missing as I suspect that it has something to do with its a xxxx.h file. If I remove the a1.cpp file from the project on snippet 1, then it compiles fine. Clearly what I have expected is not what I am getting. Theres plenty of books and tutorials on how to code in cpp, but not much in the way cpp handles files and source code in the complition process. What on earth is going on here?