Let’s say I created a basic character class for C++, I get a .cpp and a .h. What do they both do?
This answer is for C (since it the same on that matter) and C++ in general
First of all you need to know that in C++ things can me declared and defined separately, you can declare classes, structures and functions first and then add body (code) to them later, but you can do both in on same time.
But C++ compiler compiles every file individually, on each file starting from 0 and it does not have central database with information about code in other files, each file is compiled in to so called object file and later they are combined by linker in to single file, it can be combined in to object file (which is sometimes done to reduce compilation time) or be combined to create final executable or dynamic library (exe or dll). So in that case, how compiler knows what is in other files and what liberies the have when it starts with 0 information about them on each file it process? Very simple… you just need to declare functions and classes etc. over and over again for each code each file individually, so it has information how code in other files is structured… i’m not joking thats how archaic it is (but it actually this gives more low level control).
So to not paste same stuff over and over again which would be very annoying, to avoid that general practice is to divide declerations and definitions (the body which is the code it self) in to separate files, code file (cpp) with code and header file (h) with declerations for the code in cpp file. Impotent to know is fact that compiler treats those files the same, if you put code in header file it will be compiled, if you put declarations in cpp file compiler will acknowledge them normally. Now to make this works, theres preprocessor command called #include which (and this is something that lot of people are not aware of) virtually pastes other file to point where #include is (thats why order of #includes is as much as impotent as order of code by itself), it does not matter if this is header file or cpp file it simply paste content of a file to that point. So know you may figure how all of this works, to avoid repasteign declerations of all APIs that gonna be used in the code file so compiler know how code in other file ot some binery is structured, you simply paste declarations with include and single header file can include more header files so sometimes you just need to include one header file to include them all (for example #include “Engine.h” will include most of engine code declerations). Again, compiler only needs declarations to know how class or function is structured and to know that some varable exists, it leaves information about what is used in code from thsoe declarations and later linker properly link things together based of that information, creating final exe or dll.
So in order for you code to be accessable in other code files you need to keep code and declarations seperate and include declaration of that code to specific file. Keeping them seperate also prevents code duplication, since as i said compiler will compile any code in header file same as cpp, if you would keep everything toghther you would either have duplicated code in object files… if you are lucky, because linker should detect that duplication and be confused and throw you a error.
You may figure out now that each library needs header files to be used in the code and indeed in unix-like systems like Linux there central place for them and if you install library header files are included there, in Windows VS stores them somewhere to and when you install some SDK the include path is added in compiler configuration to access header files. But header files are not needed to do calls to dynamic libraries (dlls), depending on OS sometimes you can call functions directly to binaries, but it usally more dirty way of doing things
You can go wild with that in other C/C++ project, but in UE4 use of header files is more strict. UE4 has something called UnrealHeaderTool which process header files to gether information about the code, what class what functions it has what property it has, if there any structures. UHT only process declarations that is prelude with UMACRO (you knwo UCLASS(), UFUNCTION etc.) the rest is ignored. It will only accept one class per file so each header file can only have one UCLASS() which can be only UObject class, the rest can be used infinitly (well maybe there is a limit). So each UOBject file should have it’s own .cpp and h (normally you can place multiple classes, and you can still do that with non-UObject classes. UHT based from header file information and meta data in UMACROS generates extra code which register code structure information to so called reflection system and also add extra helpful stuff (like ClassStatic() which return UClass* for that class). Also if you gonna use any UMACRO in header file you need to add #include FileName.generated.h so generated code is included during compilation, you also need to add GENERATED_BODY() inside class declaration compiler also includes some generated stuff to it
Compiler process everything in to binary machine code (CPU code practiclly) where all function and varables have random memory addresses assigned and there no information stored which address is what, but code using those addresses simplyu works. To know what is what code need to be programed to understand what address is what store information about so it can refere to classes, function and varables, see reflections of it self… thats hwy it need reflection system and thats why all of this is needed, If you ever gonna dive in to code editor programming, if you ever see non-UObject class and you know wngine need to know about it’s existence, there 99% chance you need to register that class somehow with some function on module start up, but generally gameplay runtime code don’t need that reflection system and UHT do that job for you.
So in short in UE4 in header file you declare the class and in cpp you place code for it
Sorry if this is too much text for you ^^; but if i explane stuff i try to explain everything from basis so reader understand everything. It’s actually 2nd time i write this, on first try i got page not found error and lost everything xd
A little much, but thanks! Also thanks for the short answer at the end.
Thanks for this comprehensive explain. That was exactly what I wanted to know.
PS: Next time write your answer in a notepad for example, then copy-paste here to avoid accidents like this.