My app is working correctly, but I'm trying to clear up something. When I include the precompile header in my source files, I get the following happen.
Basically the first line is underlined as if there is a problem with it, so I have to include it again underneath, with the correct path... but I need both or it wont compile.
Any ideas?
The Visual Studio uses precompiled header only if the name matches (the name is not case-sensitive). Let say, a precompiled header name StdAfx.h is set in your project, so only when a code contains #include "stdafx.h" then is precompiled header used.
The #include "..\stdafx.h" is not recognized as the precompiled header even though it is the real location.
You need to add an include path to the location of stdafx.h into your project settings and then the header #include "stdafx.h" will be work correctly.
Another common issue with a precompiled header is that the include directive is in a header. It should be always in the source file (.ccp).
Related
I am trying to introduce precompiled headers into my project because of long compile times in my project right now. I have a main project that includes a main function and all the real code is in the DLL project(they are linked together.) I named my precompiled header "vpch.h" and i created a vpch.cpp that includes vpch.h. I am using visual studio 2017 so I went into the properties of vpch.cpp and selected Create. Then I added vpch.h as the first thing in all my cpp files. All files are set to use precompiled headers and reference vpch.h Every CPP files throws the error:
Error C1010 unexpected end of file while looking for precompiled header.
Did you forget to add '#include "vpch.h"' to your source?
I am at a loss as to what to do because I have followed many tutorials and can't find a reason for this. As well as most issues that pop up on google are people just accidentally including precompiled headers.
My only thought is that maybe in the section in properties where it asks for the name of the precompiled header, I need to do more than put "vpch.h" and maybe an exact file location? Any help with this is super appreciated.
EDIT
From further debugging it would appear that all but ONE cpp file is throwing an error. The one that doesn't throw an error is the one that exists in the same exact folder as the vpch.h. So files that can't just write #include "vpch.h" and have to write something like "../vpch.h" I can write #include <vpch.h> and I am going to try that now but I am unsure that will help.
The issue was with every CPP file that wasnt in the same folder as the precompiled header.
So if you use a file structure that contains different classes in different folders, using
#include "../../vpch.h" will actually fail. You must add the root folder to your additional include directories and then use #include <vpch.h> for all files. I can NOT tell you why using #include "../../vpch.h" wasn't working. It seems counter intuitive for it to fail in my opinion.
It may be because it searches for the precompiled header in the same folder as the file you are referencing it in. This answer, however, will work as a solution.
So I have been trying to learn cpp and I was writing a program, and when I try to build the solution, it gives an error saying
unexpected end of file while looking for precompiled header. Did you forget to add #include "pch.h" to your source?
Then I included it and I got the same error, and also another saying
cannot open source file pch.h
One option, if you are new to c++, is to just turn off pre-compiled headers in the project settings.
It needs to be the first include, you can't place it under other includes.
Your .cpp file is probably not in the same directory as pch.h
Try adding the directory that your pch.h is in to the additional includes, even if it is at the root of your project.
quick solution to a frustrating issue when trying to add .pch to an exisiting project:
if you have a /include /src dir structure, it might not work,
unless you place the "pch.h" and "pch.cpp" in the same dir /src.
Also: mark the "MyPreComp.cpp" as /Yc - create,
and in the .cpp files you want to use the .pch set them to Yu - use.
#include "pch.h" as the first #include in the .cpp
NB. You need to set "not using precompiled headers" to all .cpp files not using them,
yes, it IS a hassle.
( Visual Studio 2019 )
It needs to be included to each cpp file (by default)
It needs to be included in the very first line of your code (excluding the comments, it's ok to have the fancy comments on top)
It needs to be in a reachable directory. This error often happen when you have a folder structure in your project. So this can happen with a source files in some nested folder, when your precompile-header-file is up there in main. In this case, either add necessary number of "../" before the file name, or add the main folder to the "additional include directories" as it is already suggested above.
It needs to actually be the same precompile header file, that is set as the one in project setting. Check the file with "Precompiled Header" option set to "Create (/Yc)", ensure that it refers to he same header file, that you include ("pch.h" or "stdafx.h" by default) This error often happens when you include some old source to newer proj, or vice-versa, due to different default names in different studio versions: "stdafx.h" vs "pch.h".
If all above is set up, and you still have it, check if you actually set it up for the right build configuration. Always apply project setting change for all configurations. Costed me some nerves when I did it for only one config, and was trying to compile another:
I encountered this problem, Usually you all are going to ask me the why, so I'll explain first so you can just answer me, I'm working on a source with at least 500.000lines but now one of my source (.cpp) files I just created must NOT add the precompiled header which is "StdAfx.h" in this case, I must NOT edit the project precompiled header settings, obviously if I try to compile my the project without including stdafx in the new file i'll be smashed with a C1010, what I've tried is this
StdAfx.h:
#ifndef __ABC123
//do all your includes
#endif
MyFile.cpp:
#define __ABC123
#include "StdAfx.h"
#undef __ABC123
Althought, this doesn't works, stdafx.h is still including the files. Why I can't have stdafx.h includes on this .cpp file is because some includes on stdafx have tons of conflicts with the includes of this cpp. I can't modify the stdafx includes because thousands of other files use it, how to deal with this?
Impossible. You have painted yourself in to a corner here. Let me 'splain.
PCHs are intended to be used across an entire project. Every translation unit is expected to include the PCH.
This behavior can be overridden, but only via the project settings for the translation unit in question. You have already said that you must not edit the project settings, hence eliminating your only avenue of escape.
If you really need this and there's no way out, I would consider taking such drastic steps as moving the non-PCH code in to its own project.
I have library that consist of many directories each of them contains libX.cpp and libX.h files where X is directory name. One libX might be used in different projects. Problem is that while compiling each project asks include stdafx.h in libX.cpp. I suppose I must include stdafx.h file that was generated for current project (correct me if I'm wrong). Ok, I'm writing #include "some_absolute_path1\stdafx.h" in for example libA.cpp file.
But I'm using the same libA.cpp in another project and then I must change line
#include "some_absolute_path1\stdafx.h"
to
#include "some_absolute_path2\stdafx.h"
It is not very comfortable to change stdafx.h path while switching between projects.
What is best way to deal in that situation?
You can simply not use stdafx.h. It is only used for precompiled headers.
You can do #include "some_absolute_path2\libX.h"
If you still want to use precompiled headers you just #include "some_absolute_path2\libX.h" in the stdafx.h of the project that needs the library.
Any include is required when your lib is using some of the resources defined in that file to be included. If you need to include stdafx.h is because you have some dependency that is forcing that. You should review what causes you to require this project related file to be included in your libraries (not specific of the project)
I found this article: http://en.wikipedia.org/wiki/Precompiled_header
I then would try to move stdafx.h to be the first include in your main.cpp where you also include your libx.h files.
Looking at an open source code base i came across this code:
#include "StableHeaders.h"
#include "polygon.h"
#include "exception.h"
#include "vector.h"
...
Now the StableHeaders.h is a precompiled header which is included by a 'control' cpp to force it's generation. The three includes that appear after the precompiled header are also included in the StableHeaders.h file anyway. My question is, are these files included twice so that the code base will build on compilers that don't support precompiled headers? As im assuming that include guards/header caching will make the multiple includes redundant anyway...
EDIT btw, the stableheaders.h file has a check for win32 (roughly) so again im assuming that the includes inside stableheaders.h wont be included on compilers that don't support precompiled headers.
Compilers that don't support precompiled headers would just include StableHeaders.h and reparse it every time (rather than using the precompiled file). It won't cause any problems neither does it fix any problems for certain compilers as you asked. I think its just a minor 'mistake' that probably happened over time during development.
I think you yourself answered the question question! Pre-compiled headers is a compiler feature. If the guard is present the headers will not be included twice, in any case.
The only reason I can think of to protect the precompiled header and include the stuff anyway is speed. The reason to use precompiled headers is to speed up the compile times, this works by including and compiling the contents of the precompiled header, when you do this you can include headers that are only used by 75% of the source files and it is still quicker than no precompiled headers.
However if the other platforms down support prepcompiled headers you only want to include the header files that are required for this source file. So if the precompiled header contains include files that are only required for by some source files it is quicker to just include and compile the header files you need.