0

I am just beginning my journey in learning c++ with a view to creating games in Unreal Engine.

I have some great tutorial videos, which I hope to follow the whole series but I am stuck already on the very first one :[

I do have quite a bit of knowledge of C# and Java.

Basically, in the tutorial they make a HelloWorld! Windows Console App in Visual Studio 2017. The very first line in their code is '#include "stdafx.h"' , in my code there is '#include "pch.h"'

I understand that these precompiled headers give me access to specific code/functions within namespaces, but where is this code stored? and why if I add include stdafx.h to my headers does it give an error saying Source code cannot be opened?

Big T Larrity
  • 221
  • 2
  • 9
  • 1
    There's no standard way to name a precompiled header file. If yours is named pch.h, great. "Where is the code stored?" I'm not sure what you mean. – AndyG Jul 19 '19 at 20:01
  • 1
    You'll find your answer [here](https://stackoverflow.com/a/52297432/485343). – rustyx Jul 19 '19 at 20:02
  • ah, i kinda see now... basically i am massively wrong about what they do. I thought they where like 'using' or 'import' in c#/ java – Big T Larrity Jul 19 '19 at 20:02
  • thanks Rusty! @rustyx – Big T Larrity Jul 19 '19 at 20:03
  • 3
    If you are a beginner, avoid precompiled headers completely. They are an optimization intended to reduce compile time, but unless you have a *big* codebase (like 100000+ lines of code) they won't provide you with any serious benefit. But they can cause a lot of tricky issues. So I would advise to just completely avoid using precompiled headers until you have a *real* need to use and learn about them. – Jesper Juhl Jul 19 '19 at 20:03
  • hero! you just answered my comment below the answer :D thanks guys, i can move on to nex tutorial hopefully more fun than Hello World :S – Big T Larrity Jul 19 '19 at 20:06
  • And just to confirm, the "where is code stored" part was about the code within namespaces (eg. ). Thanks – Big T Larrity Jul 19 '19 at 20:08
  • Guys please help. I deleted the inlcude pch.h line, now it gives this compile error: Severity Code Description Project File Line Suppression State Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? HelloWorld c:\users\main user\source\repos\helloworld\helloworld\helloworld.cpp 21 – Big T Larrity Jul 19 '19 at 20:10
  • 1
    @BigTLarrity : https://stackoverflow.com/q/7261707/5910058 – Jesper Juhl Jul 19 '19 at 20:38

1 Answers1

1

Precompiled headers is simply a way to cache compilation when you want to compile the same headers for multiple .cpp (translation unit files).

So if you have

a.cpp
b.cpp

and these include, say,

<windows.h>
<iostream>

Then, only one compilation of these include headers will be done (for the first translation unit), the other translation units will reuse the compilation, as long as they include exactly the same stuff - that's why they stop to a specific file name. By convention, this file name is stdafx.h

So usually I have a stdafx.cpp, in which I specify "create PCH" and the others use it.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • ah ok, so would it be ok for beginning for me to delete those and simply #include all the things i need inside each file (and then later I can learn truly what the "pch.h-etc" does by using it on my own code)? – Big T Larrity Jul 19 '19 at 20:04
  • 1
    No. Make it a habit to use them from the very beginning. It speeds up compilation a lot. – Michael Chourdakis Jul 19 '19 at 20:10
  • ok thanks i will. I tried removing the line and got this error anyway :S Severity Code Description Project File Line Suppression State Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? HelloWorld c:\users\main user\source\repos\helloworld\helloworld\helloworld.cpp 21 – Big T Larrity Jul 19 '19 at 20:12
  • You have to remove the PCH option from the compiler options. – Michael Chourdakis Jul 19 '19 at 20:14
  • ok thanks , i dont even know where they are yet but will have a good read about for it. Thanks for all your help – Big T Larrity Jul 19 '19 at 20:16
  • 1
    Right click the project, Properties, C/C++ options, Precompiled Headers. – Michael Chourdakis Jul 19 '19 at 20:17
  • ah wow thank you. Maybe this won't be so important when I use UE4, but I wanted to understand every line of the hello world. And now I pretty much get it, there was a pch-h.cpp and pch-h header file in the project, also the project settings where you just linked me too had reference to the pch header file. I wondered how that include statement meant anything. Right im off to try make Pong now :D – Big T Larrity Jul 19 '19 at 20:21
  • @MichaelChourdakis "Make it a habit to use them from the very beginning" - I disagree. Not about the usefulness of PCHs as such, but that they should be used by beginners. They can sometimes cause some *really nasty* compilation errors that can be hard to resolve. No need for a beginner with a codebase in the just hundreds of lines of code range to have to risk dealing with that. Learn the language first, learn hacks like pre compiled headers later (is my advice). – Jesper Juhl Jul 19 '19 at 20:43
  • @JesperJuhl What are the nasty compilation errors they can cause? Especially for a small project, that should not be a big issue, a complete rebuild will suffice. – Michael Chourdakis Jul 19 '19 at 20:58
  • @MichaelChourdakis Not a comprehensive list, just what I can recall off-top-of-my-head: Some build systems fail to track changes to PCHs (arguably a build system failure) - I've seen this with badly written Makefile's and `SCons`. [C3859](https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c3859?view=vs-2019). If you put the PCH include as not the first one in a source file you can get some funky build errors. If you move a project using PCHs between machines or switch compilers and don't realize you need to regenerate the PCHs you can get problems. And more. – Jesper Juhl Jul 19 '19 at 21:11