Handmade Hero»Forums»Code
Sandy
7 posts
How Casey uses Headers and Source Files?
Edited by Sandy on
Hello, I am new to C/C++ and I have some doubts about the way Casey uses the headers and source files.

In most examples I have seen, people tend to use the .h file to declare stuff and usually, the header has some sort of include guards (#pragma once or #ifndef). Then the .cpp or .c file usually #includes its own header and defines de functions and stuff.

But Casey does it in a different way I think:
-He usually does not use include guards (#pragma once or #ifndef)
-He does not #includes the header in the source file
-He defines some functions in the header and other functions in the source files.
-He sometimes #includes .cpp and sometimes .h


could someone explain why?

thanks
Roman
9 posts
Been around ever since CPU's had 3 registers! (AtariXL800) That is a long time to stay a kid.
How Casey uses Headers and Source Files?
Casey uses a method called 'unity build', where there is only a single code file being compiled.
All the other files are essentially header files.

Now as far as compilers go, and files imported in the #include pragma are just inserted as is in the point where the pragma is set.
There is no (as far as I know) special consideration on what file type you are including, as long as it is text.
The compiler will just pull in the included files and expand its content.

The main reason to use this, is compilation speed, a compiler is by an order of magnitude much faster when compiling one huge code file vs. the school taught "modular" coding standard of multiple code and their dedicated header files.

As for multiple include guards, you don't need them in a properly constructed unity build because you don't have circular includes.
Each file should have a dependency only on files that are included before it.

Hope this helps.
Mārtiņš Možeiko
2559 posts / 2 projects
How Casey uses Headers and Source Files?
Edited by Mārtiņš Možeiko on
In short - he is using unity build. Unity build means that compiler will compile only one file. This one file will have everything that your program needs - all the defines, all standard includes, all the functions, all the structures and all the global variables. So how he organizes .h and .cpp files is pretty irrelevant - he just puts stuff in them only to make compiler happy (so compiler sees every declaration for the code that needs it above).

He actually uses header guards. #ifndef style.
He includes header in source file. Maybe sometimes he forgets it, but everything still works because of unity build.
Defining some functions in header file is actually pretty common in and needed feature. You need to do that if you want compiler to potentially inline it in other translation unit (unless you enable link time code generation optimization).

In C/C++ splitting stuff between h and cpp file is not required from compiler point of view. It's just that people do that because either they are told to do that, or because its sometimes easier to organize stuff like that. But from compiler point of view there is only one file - called translation unit - that you pass to compile. I recommend you to watch Chat 13 episode where Casey talks about .cpp and .h files and how compiler operates on them: https://hero.handmade.network/episode/chat/chat013 It should explain you these things pretty well.

As for multiple include guards, you don't need them in a properly constructed unity build because you don't have circular includes.
Include guards are not only for circular includes. But for multiple includes (A includes B, but C includes A and B).

Each file should have a dependency only on files that are included before it.
This is not always possible. For example, because of recursion - you need to forward declare something, and sometimes that happens between multiple "header/cpp" files.
Sandy
7 posts
How Casey uses Headers and Source Files?
Thank you so much for your help! I will read about unity build and watch the 13 episode.

I was wondering if
Casey uses a method called 'unity build', where there is only a single code file being compiled.
All the other files are essentially header files.
what is the reason to use .cpp files? why he does not uses always .h?

Could you give me an overview of how dows Casey organizes the code, how does he #icludes files. especially why sometimes he uses .cpp and sometimes .h?

sorry for the noob questions and thanks for your help :)
511 posts
How Casey uses Headers and Source Files?
Some people call is "single translation unit" (or STU) build.
Sandy
7 posts
How Casey uses Headers and Source Files?
Edited by Sandy on
A different but related question:

I am using Visual Studio as my IDE, do you know how can I tell the compiler inside VS only to compile the main.cpp file? because I think that by default it will compile all the .cpp files in the project, creating multiple translation units
Mārtiņš Možeiko
2559 posts / 2 projects
How Casey uses Headers and Source Files?
Edited by Mārtiņš Možeiko on
There are two different things you can do in VS:
1) for every .cpp file you don't want to compile, select its properties and say "yes" to "exclude from build" setting.
2) add only main.cpp to project, and use "View all files" button in solution explorer that will simply show all files in folder. No need to add anything afterwards to project: https://i.stack.imgur.com/NmkhK.png

what is the reason to use .cpp files? why he does not uses always .h?

Casey said this helps him organize code. He uses .h files as something like a documentation for .cpp file. So instead of reading .cpp file code to understand what this file does, he puts type and function declarations in .h file so it is easier to see on what data structures its interface operates (types/structures) and what functionality it contains (function names).

how does he #icludes files.
This one is easy to understand - he #include's files until everything works (that is - compiler is happy, and doesn't produce error). Casey had repeatedly said that doing this is stupid, and compiler should do that automatically. Because of how he builds his code (unity build), it really should be possible to automate by compiler, but no C/C++ compiler today provides such functionality. But this is pretty much how C# or Java compilers work. You give them bunch of files, doesn't matter in which order, and they just compile everything and then link everything together - no need to explicitly include anything.
Sandy
7 posts
How Casey uses Headers and Source Files?
Edited by Sandy on
thank you for your help :) I'll try that on VS

mmozeiko
Casey said this helps him organize code. He uses .h files as something like a documentation for .cpp file. So instead of reading .cpp file code to understand what this file does, he puts type and function declarations in .h file so it is easier to see on what data structures its interface operates (types/structures) and what functionality it contains (function names).


If this is the case, I don't understand why he often adds the body of the function in the .h file. Do you know why?
Also I don't understand why sometimes he #includes stuff in the middle of a file?
Mārtiņš Možeiko
2559 posts / 2 projects
How Casey uses Headers and Source Files?
If I'm not mistaken he does that for very small functions only. Like simple getters, or simple "constructor" functions that initialize structures. Or some small helpers like IsXyzProperty, IsObjectsEqual and similar type of Is... functions.
Technically there's no reason to not put them in .cpp file in unity build. Maybe its just a habit to do that from "old times"/previous experience when using multiple translation units. Because for multiple translation units it makes sense to create small inline'able functions which you need to put them in .h file.
Sandy
7 posts
How Casey uses Headers and Source Files?
Could this has something to do with the fact that these functions are inline?
Mārtiņš Možeiko
2559 posts / 2 projects
How Casey uses Headers and Source Files?
For non-unity builds - yes. For unity builds it doesn't matter much.
Sandy
7 posts
How Casey uses Headers and Source Files?
mmozeiko
For non-unity builds - yes. For unity builds it doesn't matter much.


I understand

Thanks a lot for your help!

Just a final question: do you know if there is a way to set "exclude from build" to "yes" by default in VS?
Mārtiņš Možeiko
2559 posts / 2 projects
How Casey uses Headers and Source Files?
Do you mean how to set this setting to Yes when you add or drag&drop new files to project?
I don't think there's a built in way to do that. It probably would be possible to write some extension to do that, but no idea how... I usually use second approach - just view all files in folder and don't add .cpp files to project when doing unity build.
Sandy
7 posts
How Casey uses Headers and Source Files?
Oh I see, thank you! I'll do that :)