Precompiled Headers in C++
Vložit
- čas přidán 8. 09. 2024
- Patreon ► / thecherno
Instagram ► / thecherno
Twitter ► / thecherno
Discord ► thecherno.com/...
Series Playlist ► thecherno.com/cpp
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K
Aaaand done. I've watched every single episode of this series, almost 15 hours in total. C++ has never been clearer to me, you're so good at explaining! I have an exam in C++ in about a month and I'm sure I'll do just fine. Thank you, Cherno!
How did it go?
@@pilotandy_com He failed.
Chappy made my day dude lmao
Me too, I've learned a lot
@@chappy1835 LOL
I wish I could precompile all your c++ tutorials and load them directly to my memory lol.
*to my disk
would be better :)
Nice
no hard loading, just create a pointer is easy
@domidivo Jdownloader can do this for you
Oh yes more c++ juice
Ciccio&Bombo
Lol
yammy
That sounds weird but yeah.
mmmmmm Nice bath in c++ enslaved moisture
g++ precompiled headers? Awesome videos. Joining the Patreon crew. Your videos are better than most c++ books I've read!
what's g++?
@@passionfruit7617 one of the main C++ compiler for non-Windows platform (with clang, although clang is more macOS while gcc and g++ more linux afaik)
@@passionfruit7617 It's stands for GNU C++ Compiler
(GNU stands for GNU is Not Unix, and is an operating system that was **kinda** discontinued but its compiler collection (GCC) has become widespread)
g++ -> c++ compiler
gcc -> c compiler
well g++ should be the same as gcc
thecherno > my college teacher :)
So you're free to worship him from now on :)
thecherno = mycollege
@@neo-babylon7872 nonono,
the_cherno = my_collage;
@@theairaccumulator7144 How do you misspell college when you literally just read it
@@theairaccumulator7144 Wrong again, theCherno = myCollege; camelCasing FTW
I've been following Your channel for like a year now, and I have to say, every video that you make is extremely high quality, both visually and content wise.
For those using g++, be sure to use the exact same g++ compile options for your precompiled headers as when you compile your project.
Also if you're unsure whether your header or the PCH is being used when you compile your project, try adding the -H option when compiling your project. The -H compiler flag will print out which files are compiled, including each header.
If the PCH is set up properly you should see a line like "! pch.h.gch" The exclamation point (!) indicates it was successful. If instead you see an X, that indicates there was a problem with the PCH.
If you don't see this line and instead are presented with a wall of text printing out each header that was included in the compile, then your header was used instead of the PCH.
I've been using them for a while and my first impression was that they are awesome, I just wasn't aware of their potential problems and I'm glad you mentioned them here. I'd like to add that if you are having trouble figuring out header file dependencies when using a precompiled header you should use the /showIncludes compile switch if you're using the msvc compiler. It will print out every header file dependency as they include each other. It's very very useful!
And thats it! I have completed this C++ series! And I have to say compared to what I learned in almost an year at my university, I learned more in a week following this series 😃. Thank you cherno for this amazing series and i am looking forward to watch game engine series and others as well! You are my fav teacher from now on 😁. keep up the great work, you are the best!
I took a semester of c++ six years ago. I was surprised how quickly he eclipsed everything I learned.
18:42 precompiled headers with GCC
(no fancy stuff lol, just a regular compilation)
Started using Windows Console Application template in VS and was wondering why the pch.h file was auto-created. Thanks for the information! This helped a lot.
this is awesome. thank you brother.
Dude, your vids kick ass++
Thorough, informative, concise
This time we get all three
Thank you for your time and efforts
Ding! Ding! Ding! This is Mr. TheCherno's best video. Back in the 1980s/early 90s when computers were really slow, my Symantec C++ compiler had precompiled headers which really improved compilation speeds. Now computers are super fast so compiles don't take long, except in one instance - when compiling source that uses windows.h and is stored on a network drive rather than a local drive where it gets cached. I could have been using precompiled headers for the last 30 years and will now investigate if it will provide significant time savings.
Of course I have some criticisms but they are small compared to the positives of this video. It took Mr. TheCherno 12 full minutes to say "you don't want to sit around waiting for constant code to compile, use precompiled headers to reduce wait times." I don't care about flipping screens and filling in blanks in Visual Studio, show me the command line so I don't have to research it.
It hadn't occured to me that general C++ tutorials would still be coming alongside game engine tutorials.
And I'm still learning new things. Why does c++ have so many "hidden" features? I've never heard of precompiled headers.
It's technically not a feature of C++. It's a feature of certain compilers such MSVC.
here's an easy way in vs to force include your pch.h file in every source file:
Configuration Properties -> C/C++ -> Advanced
Type "cph.h" in the "Force Include File" field; this will make the visual studio compiler insert the header file at the beginning of each file without having to include it manually.
Tried, doesnt really work when u edit cpp file, it wont find stuff without including header manually
@@TheDezmen3 hmm for me it works, did you set it for "all configurations" ? Just checking
Modules are one of prominent features of standard C++20. They overcome the restrictions of header files and of the preprocessor usage. I would recommend to pay closer attention to this feature since it will eventually boost C++ packages management.
Correct me if I am wrong but the compiler does not compile the whole std::vector just because you included it. The only template code thats going to be compiled are those that you use (instantiate). So for example if I instantiate a vector and use the push_back method, the only code thats going to be generated are these 2 functions.
The issue isn't necessarily the code generation, but figuring out what code needs to be generated. The pre-processor still needs to parse the entire code unit to figure out what actually needs to be compiled.
Wouldn't it be nice if every function was just available, and only included in the compile if called... How can I #include everything? I've got a fast computer.
This is correct. Many compilers don't even parse the template (aside from finding the end) until it does get instantiate. Which can cause the awkward situation that certain syntax errors won't showup unless you instantiate.
a cmake video please D:
Or say: Do this in a VS-Free way.
@@GfastGao VS currently integrates cmake
You can add a PCH to a target in CMake using "target_precompile_headers" like so:
target_precompile_headers( PUBLIC | PRIVATE | INTERFACE
[["header_name_1.h"]]
[["header_name_2.h"]]
[["header_name_3.h"]]
...etc.
)
Only works if you pass in a compiled library or executable target.
You can also reuse precompiled headers in later targets using the following:
target_precompile_headers( REUSE_FROM )
Maybe you could make a video about unity builds and the differences (positives, negatives) compared to the more common way of building programs.
For XCode user:
→ Your pch file should be in the format -Prefix.pch
- Go to Build Setting
→ Turn on
→ Add the path to your pch file in
You don't need to include it in your *.cpp file, it's automatically included.
What an awesome series of videos! I reviewed the whole series from start to finish over the last 3 days and looking forward to starting the OpenGL series next! Thanks a million for the fantastic material.
Thank you, Cherno, for making those wonderful videos. You are benefiting people and contributing to a better world!
Absolutely perfect. Thanks for doing this.
Ahhhhhhhh Adding this to my project felt good. Every tutorial ever always says not to use PCH, probably just to avoid the complications, but man alive, it was nice pch to my personal projects. That plus enabling multiprocessing builds in VS took my clean build time to 2.2 seconds. Was 5.7 seconds before this. That means a lot to me. I just love to compile all the time.
hey, do you by any chance know how to enable multiprocessing from premake? I looked around but couldn't figure it out : /
Nevermind, just found it, lol. In case anyone cares, it's very straightforward, just add:
flags { "MultiProcessorCompile"}
Just finished your C++ Course. I hope you'll keep posting more videos about C++ because I've learnt so much thanks to you. Don't abandon this series. :D
cherno, more like cheryes
I was searching for a way to speed up building my codes, and PCH will probably have a huge impact. Thanks again for the tips and tricks.
I was scared of precompiled headers and I've always disabled them in the project settings. Now I see how simple they are in fact, so I'm definitely going to give them a try. Thank you very much!
Thanks so much
I've been using them wrong for years lol. At my last job our tech lead had us throw everything in there. Having never really used them before, I didn't question it. Makes total sense now! Thanks for the video!
Perfect series, Thank you Cherno :)
Can you make a video about async calls. I have an embedded systems background and because MCU's are single-threaded, async calls are used, thus presence of async libraries, I am interested in developing libraries i use by using async calls.
When I use precompiled headers, I always still write the includes in the cpp files and just comment them out :)
Phew, I've finished the series! Thank you a lot for sharing these and I wish you having a happy life there!
Thanks a lot 😁
Man after following this series for a while I'm confident that I can get to building my first game in C++ soon
never even heard about that
i m a last year CS student from Ukraine and i just got my first job as c++ programmer and i'm pretty sure i wouldn't have passed all the tests and the interview if it wasn't for your c++ videos
thanks a lot)
Can you please add video on how to write cache friendly code in C++? Bad and good design examples would be great.
Hearing from them the first time, and I think they are great. It's a little sad, it's not done automatically by VS etc...
This is 2021, August, I have never heard before about precompiled headers... Thank you for introducing them though, this is as if I would already be a kind of a member of a kind of a learning company, and you as a kind of senior co-worker explain the tricks of the old foxes. As if.
Stupid arm thing at the beginning of the videos is everything
Is there a way to use more than one precompiled header file in a MSVC++ project?
It would be nice to separate the external headers that (almost) never change from internal headers that rarely change, but which do change from time to time. Then if/when I do have to change some rarely changed header, at least I wouldn't have to wait for all the external headers to recompile.
Aside from combining translation units into one, are there any ways to further mitigate per translation unit compilation delays?
I tend to end up with a ton of .h and .cpp files, and far more often than I'd expect, all or most of them end up getting recompiled when I run the build. In watching the compile, it definitely seems like there is a non-zero compile time cost associated with just having a translation unit at all, even if it has hardly anything in it. So I have this tendency to create "bundle" .h and .cpp files, that just #include a bunch of constituent .h and .cpp files, and then exclude those constituents from the build, so that way there are a lot fewer translation units to build. It definitely speeds up the build, but. I feel like there has to be a better way.
Is there a good, succinct resource for reviewing the hows and wherefores of headerless C++ coding?
Is there a good, succinct resource for reviewing the nuts and bolts of building Zero Overhead Abstractions?
Recently I've been re-reading Bob Martin's book, Clean Code. One of the Big Ideas in it is to keep classes and functions really super small by dividing big ones up into smaller ones. I've tried that before, and I've kind of discarded the idea, because it seems like it's a lot more trouble than it's worth; but now that I have a lot more coding experience in general, I'm tempted to maybe re-approach it. But performance and efficiency are my foremost concerns, and build time is another big concern, and time spent coding tons of little classes and functions is another matter that kind of still gives me pause about it all. I think I need to better understand how I can
- avoid having to maintain separate declarations/headers for every little constituent class and class member,
- place their scopes into physical hierarchies (since named functions can't be nested in C++) without proliferating too many namespaces / long ns chains,
- organize and control their individual scopes, both cognitively and in terms of their impact on the build, and
- build zero-overhead abstractions.
There's a fair bit that I already know about all that, but. If there isn't more to know than I know, then I guess I'm still kind of feeling like trying to really make super small classes and class members in the way Mr Martin talks about isn't really worth it in C++.
0:04 Hello The Cherno! Welcome back to my PC. :) LOL I made a funny!
FYI I am aware I am not funny but that will never stop me :)
This is genuinely a great video thanks a lot.
How can I add precompiled header file into a file inside subdirectory? if I use "../pch.h" it won't recognize it it needs to be "pch.h"
Can you make a video on how to precompile .hpp headers from command prompt?
Well explained, thanks!
You are a legend !!
Excellent video series!
I think he said "Right?" about 40 times lol
Right
Right!
I don't know if that's right. :)
lmao that's very close, he actually said it 59 times (at least that's in the transcript)
Right
Love your series. Could you do a video on file input/output at some point as well please?
I love the People's Elbow gesture in the beginning
Thank you. I really like this kind of video. Please more :)
Can you please also do a video on the pIMPL idiom/method and on alignment and padding?
Thanks, cherno! We are finsihed this courses!
I don’t use them for the reasons mentioned . I like keeping track of my dependencies, don’t use STL, and use a “game module” approach. True, my project is not super big yet but so far the modular design and limiting dependencies gives quick compile times.
Unreal engine also moved to the “include what you use” philosophy a while ago (similar approach in my personal engine)
@@foomoo1088 CMake's target_precompile_headers() force includes the PCH into the translation units so source files can stay the "include what you use" way. (Still means you shouldn't bloat the PCH with absolutely everything, of course).
I wonder how to use this trick in a CMake project. Is there an example?
I have make that work will but cmake i don't know
Why am I learning about PCH's only now?! I managed to reduce the time for a full compile by >75%! WOW! Thanks!
A video in the middle of the week! What an amazing surprise :D Thank you
very informative
Glad I came across your channel :)
Would love to see a video about typedef, typename, and using keywords! Love the videos
Could you please do a video on custom data structures next ?. I know you had mentioned them a while ago.
First time I've heard of it. This is one of the problems with university education. You learn tons of theory but never hear about the practical tools/tips/tricks that professionals actually need.
What about compiler cache? looks like a simpler and more ffective solution.
Yes, I think a compiler cache is a much better solution!
This is definitely a looooooooooooooooong hello world 13:29
I was really hoping that you would somehow put this into the premake file, that would be awesome
When are you going to make video on "File Handling in C++" , I can't find any fruitful video on that!
Please make a video on that!!
Moreover , I just love the way you explain things and you are really my teacher now a days.
I have my final tomorrow of OOP, and will pass it just because of you!
Thank You Sooooo Much!
Have a Nice Day!
Hi Cherno, do you work on unreal engine and will you do some video about it ?
Thx for this C++ series as well as the game engine one, it is very useful in my studies.
No, he doesn't
I believe Udemy have some excellent courses on that topic if you are willing to spend a few bucks.
@@esben181 Don't bother with Udemy, just look around on CZcams and use the official tutorials, there's plenty of free content out there.
@@esben181 Thx but I just like the quality of Cherno's video and wanted to know about future projects.
The Unreal C++ course on Udemy by Ben Tristem is really good, and very cheap, I definitely recommend it.
Thank you
very cool
Best explanation I have ever seen.
Wow! I knew there was search a thing as pre-compiled headers just didn't really know what they did.
Can you have several pre-compiled headers in one project? Maybe like that you can pre-compile external dependencies in one PCH and then have your own code (or part of it) in a different PCH. This could be useful, no?
@electroturi Yes, you can have several pch in the same project, but perhaps not in the way you are thinking. AFAIK in Visual Studio you can only make use of one pch in each cpp file. There is a caveat too, and that is that the #include "pch.h" must be the first effective codeline in that cpp file. Everything before that line is actually completely ignored by the compiler! If the lines are just comments, that's no problem of course, but if you accidentally put something else important there, you will eventually be bitten ... My guess is that the implementation is simply such that the compiler just loads its entire internal state from the pch, then continues compiling from the line after the include pch.h. Whether it is similar in g++ and clang, I don't know, but that would be the reason you cannot load several pch in the same cpp with MSVC at least.
Potentially stupid question:
if I add a lot of files into the the precompiled header, and then I include the pre compiled header, am I not including a lot of potentially useless code into every file of my project and yes speeding up the compile time but also increasing the final executable file size?
Will turning off precompiled headers for say a release build make the compiler ignore included files that are not being used, therefore I can expect a larger executable file during development, but a smaller executable in release?
Or the difference in sizes is so minimal I can avoid caring at all, considering large projects too of course.
They used to lead to project folders taking up huge amounts of disk space (in Visual Studio, at least) - eg a simple Hello World app could take up gigabytes. Is this still the case?
Waiting for more videos! CMake is a good choice.
Make a video about set and unordered_set in C++. I need them for a project at my university. Especially how to implement them for a custom class/structure. Or, maybe give me a good source from where to learn this, because I've had hard times trying to implement unordered_set for my custom structure.
compiling .h file with clang++ gives a warning:
clang-6.0: warning: treating 'c-header' input as 'c++-header' when in C++ mode, this behavior is deprecated [-Wdeprecated]
.hpp compiles fine.
New to me, it's a go for it to me
Great video!
@TheChernoProject love your videos as usual, super awesome, ill definitly try this !
personally i like to do a Makefile with the file dependency's and compile all my .cpp and .h files into object files and link them at the end of the makefile sequence, so basicly make only recompiles the ones that have changes and relink them, much faster than normal compiling.
12:11 where it really starts. And thanks a lot you were super helpful.
19:10 how did that thing generate 117 megabytes in such a small time what???
amazing video
you video are so excellent
Cover move semantics next yes
Clangd complains that I'm not using the header directly, should I just disable that warning or is there a standard way to deal with this? The fact that it's warning at all makes me feel like there's a convention for pchs that I'm not aware of
Amazing!!
another question,
1. cant we have different pch.h file for different projects
2. i see it makes difference on development time, does it have any performance on final executables also? or is that same
No need to say, that your explanation is awesome, better than many classroom teachers.
11:20 the saliva made its entrance lol
It happens to the best of us lol
from 8 to 1.5 sec.
that was my experience with pch, but it took me hours to figure out how to use them lol.
is there a way to do same with a CMake project ? Those properties you showed wont open in Visual Studio 2019 for a CMake project.
Hey, Cherno can you make a video on maps?
Before anything... Amazing videos. I love the way you explain stuff man. You're really good at that.
11:50 i know this is pretty old, and I agree that PCHs area must for those kinds of libs, but dude, you keep saying you don't want to recompile it everytime, regarding the STL... And that not entirely false, cause you end up recompiling symbol references you use and parsing the h files into your translation unities,, but the library is already compiled in the c++ runtime, or other libs/DLLs you use. So it's also not all true that you end up recompiling all that stuff. If you would have to, then you would see the amount of time that that takes, it's absurdly larger and longer than just parsing headers... That's one of the main header/source separation reasons. Unless off course you use some bad lib that needless defines a lot of stuff in headers.
I'm certain that you know all that, but saying that might be confusing for the people that does not have good knowledge about c++ or linking in general and possibly give them a bad ideia of what that whole system actually does.
I just think that it's something that should be mentioned, although, I'm not as good at teaching as you are, so I might be wrong as well haha, and this is something people shouldn't have to know at first, like how you get to physics formulas when you first learns physics cause you have no idea of integral calculus, limits, and so actually is.
I still think it should though. 😂
Is it possible to make a vid about implementing the big five in c++ when you have a pointer in a class as a data member
i love precompiled headers i hate waiting when compiling
nice
This is nice
Didn't know about this feature.