VS Always looks for precompiled headers - c++

I am Using VS Community 2015 to create C++ Console applications
I am trying to stop using pre-compiled headers.
I have deleted #include "stdafx.h" , and the stdafx .h and cpp files.
In the project properties pages I selected
Not Using Precompiled Headers
I tried clean, rebuild, nothing works.
VS keeps looking for the precompiled headers:
1>ConsoleApplication2.cpp(7): fatal error C1010: unexpected end of
file while looking for precompiled header. Did you forget to add
'#include "stdafx.h"' to your source?
Anyone have a solution?

VS project properties can be overridden on individual source file level. Inspect the properties of ConsoleApplication2.cpp (and other source files where you get errors) and make sure their PCH-related properties are not bolded, i.e. that they are in the state "inherit from parent or project defaults."
Also make sure that you are editing properties for the configuration (e.g. "Debug") which you are actually building. The VS property dialog allows you to edit properties for any configuration, not just the one currently selected in the solution-wide configuration combo. What I usually do is to always set the dialog's configuration to the "Active" option before exiting it, so that when I open it, I don't have to check.

Related

Is it OK to make a stdfx.h file myself?

Now, I realized that I need a precompiled header for my project. However, at the beginning, I created my project as an empty project which doesn't have a stdfx.h header file. So, this is what I did:
- I created a header file name stdfx.h
- In that file, I included some libraries.
- I went to the project Property Pages -> C/C++ -> Precompiled header -> set the PRECOMILED HEADER option to Use (/Yu).
- Finally, I included stdfx.h in every cpp file.
It works. However, I am wondering if the stdfx.h file that I created myself works like the one that is automatically created by Visual Studio? I mean is it really a precompiled header which will save time when compiling or just a normal header file?
It's normally named stdafx.h, but it really doesn't matter. You can name your precompiled header whatever you want.
You are missing one final step.
You also need to create a stdafx.cpp. This is an empty file that only has a #include "stdafx.h" line.
For this particular file, right click on it in the Solution Explorer and select Properties. This will bring up a Properties page with settings specific to this source file (in inherits your project settings by default). For this particular file, select /Yc as the compiler option instead of /Yu for the Precompiled setting. If you don't do this, you may not observe the build speed improvements of precompiled headers.
When Visual Studio builds, it will build your stdafx.cpp file first. And along with that, it will build the .pch file that the other source files will pick up.
And while I'm here. The things to include in the precompiled header file are all the system and C/C++ runtime header files. (e.g. <windows.h>, <stdio.h>, <string>, <map>, etc...). You'll get the most gains by including all these files - since they never change from build to build.
You definitely can make stdfx.h by yourself or other precompiled header file (name is not really important). But you should follow some rules described in MSDN.
You can read more about precompiled headers in the Documentation

C++ Precompiled Header Disabled

In Visual Studio 2015, I have a C++/CLI project with the following error:
"fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"
I turned off precompiled headers in the project options but it keeps giving me the same error. I even removed the name of the precompiled header file but it still says "stdafx.h".
Edit:
Yes, every source file indivuduially doesn't have it enabled.
Ok. Hans Passant found out what was happening:
The build configuration that I used to build it still had precompiled headers enabled.
You may have disabled it for the project, but each individual source file can have it's own specific compiler settings. It's also possible you disabled pre-compiled headers for a specific source file, erroneously mistaking that action for a project wide setting.
For each individual C/C++ file in Solution Explorer, right click on it and select "Properties". Under C/C++ -> Precompiled Headers, make sure the Precompiled Header setting is "Not using Precompiled Headers".
The right click on your Project name in Solution Explorer and select "Properties as well" Double-check that the project settings are the same.
And if there's a stdafx.cpp file, it likely has the "Create Precompiled Header" setting. You can typically remove this file if it doesn't have any relevant code in it.
Repeat all the same steps for each Solutions configuration (e.g. both Debug and Release).
I met the same problem (turned off precompiled in project properties but the error remains).
Solution:
open .proj file with any text editor and search for
<PrecompiledHeader>Use</PrecompiledHeader>
you will see that there are still some properties set to use it. Just replace word "Use" with "NotUsing" and problem fixed.
Microsoft Visual Studio 2015:
Right click on solution -> Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header => Not Using Precompiled Headers
In my case, I found the pch.h still set in: c++ -> advanced -> forced include files.
Anyhow, Andrew's solution is still the best. Just open the proj. file in a text editor and ctrl-f search for the filename causing the issue. Much easier than skimming through all the properties in vs.

What is the difference between "Create Precompiled Header" (/Yc) and "Use Precompiled Header" (/Yu) in Visual Studio?

I read the documentation in MSDN, but in the end I didn't get a clear idea what is the practical difference between them, exactly. Both seem to require stdafx.h to be added at the top of every *.cpp file.
I'm using VS2008.
Can anyone help clear things up?
Short summary of how to use PCH files in Visual Studio:
All cpp files in the project have to include stdafx.h (you can change this to something else if you wish)
Select project in Solution Explorer and in Properties -> C++ -> Precompiled Headers set 'Create/Use precompiled headers' to 'Use'. Hit Apply
While the Property Pages are still shown select stdafx.cpp in solution explorer and set the value to 'Create'
Well, I think that you must first understand the purpose of precompiled headers. In large projects, it can take ages to process all the headers required by a single client extension for example, so some people prefer to distribute the .pch files along with their libraries. In order to generate the .pch files, you use the /Yc compiler flag and the person who wants to consume your library will set the /Yu flag. See here and here for details.

Rebuild precompiled header

I am trying to use MFC with ProC.
Functions.pc is where I do the ProC syntax to connect to the database. So I include the 'Functions.h' in the dialog model where I want to call the functions in 'Funtions.h'. And now I got this error.
Add directive to 'stdafx.h' or rebuild precompiled header
I understand that I need to include 'stdafx.h' to 'Functions.h' for it to work. And I did that. It can compile,but then when I try to run the program,it won't recognize the syntax of ProC created by the 'Functions.cpp' created. I don't really understand how to rebuild the precompiled header since I don't understand the codes in 'stdafx.h'.
The common way to use precompiled headers on windows is
Include system , third party headers or infrequently changing headers in stdadx.h
stdafx.cpp usually only includes stdafx.h
All your project cpp files include stdafx.h as the first header
Your project header files should not include stdafx.h
To trigger a precompiled header rebuild,
Modify stdafx.h and do an incremental build
Or Do a rebuild project
Found the solution myself. I need to set the properties of that .cpp file to not using any pre-compiled header.
Within VS 2012 you can set the properties of the Project, the Solution, or Source File(s) to not use Precompiled Headers. See attached graphic for how to do it...
VS Turn off Precompiled Headers
In VS2017, this error persisted until I switched Project Properties > Configuration Properties > C/C++ > Precompiled Headers > Precompiled Header setting from "Use (/Yu)" to "Create (/Yc)".
For the project setting, use:
"Create" for pre-compiled headers to be produced by the project
"Use" for pre-compiled headers produced by another project
For per-file settings, use:
"Create" for the stdafx.cpp file only
"Use" for all other .c and .cpp files

Is there any way to use VC++ 2010 without including stdafx.h?

I've successfully installed M$ VC2010 and start writing simple programs using it.
I am very annoyed from the #include<stdafx.h>, So is there any way to compile and run programs without it???
It's for pre-compiled headers. Don't use pre-compiled headers, don't include it.
Goto Project Settings->Precompiled Headers.
Select Not using precompiled headers.
You can also turn off precompiled headers per file.
Setting this in VS Express C++ 2012 is as follows:
In Project Explorer right-click on .cpp file, select Properties.
On the left pane select Configuration Properties/C++/ Precompiled
Headers
Now, on the right pane there is 'Precompiled Header' value which
you can change from Use /Yu to :
'Not Using Precompiled Headers'
Cheers
Of course, just go to Project Settings, C/C++ → Precompiled Headers, and choose "Not Using Precompiled Headers".
BTW: On large projects using precompiled headers may significantly decrease build time.
When you create your project, create an empty project.
For now, you can disable it in Project Settings.