Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

Can I use #include "pch.h" instead of #include "stdafx.h" as my precompile header in Visual Studio C++?

This is my first time working with C++ and properly getting into coding. I'm following the C++ tutorials on learncpp.com and I'm using Visual Studio 2017...

In the tutorial they start off with some simple "Hello, world!" code and at top of the code they put #include "stdafx.h" along with #include <iostream>. When I replicate this code myself and try to build it, I get the error:

C1010: unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?

When looking at my solution explorer I noticed that in their tutorial in the header and source files tab they have files called "stdafx.h" and "stdafx.cpp", but for me those files are called "pch.h" and "pch.cpp".

So I then tried renaming the #include "stdafx.h" to #include "pch.h" and the code was built and executed perfectly. So should I just stick with #include "pch.h" for the rest of my code or is this some kind of issue?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The default precompiled headers name was stdafx.h for several years now. Lately, with VS 2017, they changed the default name the project wizard creates to pch.h. I don't know why but they did it.

You can adjust the name of the precompiled header file in the Project Properties under Configuration Properties -> C/C++ -> Precompiled Headers. You can also enable or disable the use of them there. You can even define this on a per file basis. You can even configure multiple different Precompiled Header files in one project.

So for you concrete question: There won't be any kind of issue replacing stdafx.h with pch.h in your tutorial. The tutorial is probably just older and hasn't been updated yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...