03-16-2018, 09:28 PM
(This post was last modified: 03-17-2018, 09:32 PM by Brian Beuken.)
(03-16-2018, 09:18 PM)Brian Beuken Wrote: "So when I include xxx.h, xxx.cpp is included also for compiling?"
not quite, you are making xxx.obj and the xxx.cpp file contains all the code, once the obj is successfully made, it is put to one side
Now suppose xxx.cpp contains the class MyObject.
the xxx.h file, should really be called MyObject.h
But it was compiled ok, that means we have MyObject.obj and the code to make MyObject's exists
If we now take another file, like our bootstrap file, containing main()
if we want to do something like
main()
{
MyObject MO; // create an instance of an obj
}
Then clearly the main.cpp file has no idea what a MyObject is... its never heard of it before, so it will complain, out of scope, or undefined.
but..
#include "MyObject.h"
main()
{
MyObject MO; // create an instance of an obj
}
tells the main.cpp file, that there are things called MyObject and now it knows what they are made of, the defintion of them is in the MyObject.h file.
The code for them is still very much in the MyObject.obj file, but main.cpp can compile and make a MyObject called MO...which it can then use. The compiler is trusting that the MyObject class is properly defined, and puts markers in the main.obj wherever there is a use of MyObject, for the linker to fill in, because its job is to take all the obj files and fill in the blanks.
When the linker comes along it has main.obj, and MyObject.obj and fills in the markers with the correct code, if the files are properly used and no extra undefined or mis defined usage has occured, the 2 objs are combined into 1 executable file and we're good to go.
Thats a simply way to think of the build process, edit, compile, link...run.
Thanks for you long answers. In my project there are two .cpp files. The GameProject1.cpp and MyFiles.cpp.
MyFiles.cpp is as follows:
#include "MyFiles.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
MyFiles::MyFiles()
{}
MyFiles::~MyFiles()
{}
char* MyFiles::Load(char const *filename, int* width, int* height)
{
unsigned char *data = stbi_load(filename, width, height, &comp, 4);
return (char*) data;
}
So this MyFiles.cpp is compiled and linked when the project is build? So that the Load function can be used. Why is the content of MyFiles.cpp not put into GameProject1.cpp? Because #include "MyFiles.h" is now in both .cpp files!