My question is how files are linked in C++, specifically in MS Visual Studio 2008 since that's what I'm using (but would be very interested to know if there are significant differences between compilers in this regard).
When you generate a new solution in MSVS you also get 3 folders delivered: Header Files, Resource Files (which I haven't used yet but would be also interested to know what it's used for), and Source Files. One part of the intention is pretty obvious: Put your .h files in header files and your .cpp files in source files.
Just doing that and writing correct code will compile and run as console application IF, when you created the project in the brief wizard, you clicked "create empty project." However, if you get in a rush (as I just did, hence reminding me of this question, which I've had for a while) and just click "finish" before specifying that you want an empty project, then MS creates several generic files for you (can't remember exactly what since I deleted them all and have no desire to repeat this mistake).
I kind of think these files are for a visual C++ project, but that's really peripheral to my main question: After deleting these files from the solution folder and just creating some new ones initially that I knew would work (I just copied some working files that I was intending to change), Visual Studio refused to compile the program and explicitly asked for the files that I had just deleted.
At the same time, on the surface, everything looked just as it had in the functioning version (except that the name of the solution folder was different). So, what I'm wondering is what the compiler, or specifically the one used in Visual Studio, uses for linking and compiling the files that goes above and beyond simply their location in the appropriate folders. So, basically, the things I would need to change if I clicked the wrong button and got some precompiled files I didn't want.
By default, it uses something called precompiled headers, which speeds up compiling of headers.
For this to work, you need a header file which it will precompile (usually called stdafx.h) and a source file that includes this (usually called stdafx.cpp) and you must also include this header in all your source files.
In case you do not want this, then you should go into settings, recompiled headers and select none. This will eliminate the need of these files.
Well, it sounds like you like it with the reasoning that it speeds up compiling of other headers.
And I feel like I'm also reaching the point where it's probably beneficial to start exploring how this kind of thing works.
Is my understanding correct that in order to use this feature, you need to put #include "stdafx.h" in every .cpp file in your solution but ONLY those (i.e., other header files don't need to #include it)?
Also, is there a reasonably simple explanation for why it doesn't compile at all if you just delete those files (and obviously don't #include them) and start again seemingly from scratch but in the same solution folder that VS created? It seems like there have to be some kind of properties somewhere that you can edit if you decide to change the way you're setting it up.
You only need to make sure you include the header in every source file. You may include any other include files you want, as well. The only requirement is that you include the precompiled header file.
Also, like I explained, if you create a solution with the default settings and want to start from scratch, you need to disable the precompiled header option. After that, it will work.