Changing project's dirs breaks precompiled file creation - c++

I use MSVC2017 (with MSVC2013 toolchain, if it's matters).
So, I have created new solution with "static lib" project and a console app. At this step it works.
Now, I changed some project's paths for the lib project:
Output directory: $(SolutionDir)BuildDebug
Intermediate directory: $(OutDir)\Debug
I added following includes into pch.h:
#include <windows.h>
#include <inttypes.h>
#include <cstdlib>
#include <stdexcept>
I added reference to my lib and lib's include dir inside console app. Now Studio cannot compile my project, showing a lot of errors about
error C2061: syntax error: identifier 'LONG'
and related.
I found that there is no .pch file anywhere.
I also created test solution with same static lib alone and changed same paths. It seems to compiling successfully, but Intellisense underlines "LONG" with red, saying "it's undefined".
May be someone faced the same problem.
Update: I just tried to create a new solution. I added static lib project, then I added simple file with single function int func(LONG v). It seems to compile. But then I add console app to the solution, link it against static lib, and it's not compile, saying that
error C2065: 'LONG': undeclared identifier
Update 2: I've found that static lib files don't see <windows.h> added into precompiled header. So I included it into my header directly and added typedef struct IUnknown IUnknown; before windows.h, because of new error related to IUnknown. It seems to work. But I still don't understand what is going on.

"Pre-compiled headers" are a build-speed optimization. If they're giving you problems, you can always turn off their use temporarily. When they're turned off, the .pch is no longer used, but the .h is still used.
I expect you'll still have the missing LONG, since it's not even in the .h
For your sanity, it might be useful to use explicit names for your precompiled .h. I'm not sure if VS2017 already uses pch.h by default, or it it still uses stdafx.h. Either way, that's just a default. If you want, you can also rename them to staticlib.h/.pch and executable.h/.pch to avoid confusion. The compiler has no default name for the pch; it relies on compiler switches /Yc (create) and /Yu (use).

Related

Custom precompiled headers causing compile errors

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.

How do I fix unexpected end of file error with pch.h

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:

C++ Visual Studio Compiler compiles native code as managed

I have started a new solution under VS that has both a managed, UWP code project and a native project. The compiler compiles the native code and delivers a .lib file just fine. When compiling the managed code, the compiler compiles the native code again -- as managed code and spits out errors.
I have another solution that use to work and I have tried to replicate the settings. After a day of comparing the property settings, I cannot figure out why it's doing what it's doing.
Ideas for where to look?
_____ New below_____
I have started a new solution an project and replicated everything. Here's what I've learned.
First, the compile order is different -- there was a missing stdafx.h file and the errors went from infinite to just 25. They are now related to one file, MotionBase. The native project compiles just fine, then the managed project compiles and it barfs on MotionBase. This code sample gives errors "'MotionBase': is not a member of 'BallLib'" and"'input': unknown override specifier".
#pragma once
#include "stdafx.h"
#include "FiniteDiffHelpers.h"
#include "MotionBase.h"
#include "MultiVarSolver.h"
namespace BallLib {
class PathFinderHelper : public FiniteDiffHelper
{
public:
PathFinderHelper();
Line locs;
BallLib::MotionBase output;
MotionBase input;
.....
PathFinderHelper compiled fine in the native project. There are no errors in MotionBase. MotionBase is part of BallLib. Intelisense gives no errors in PathFinderHelper.
Ensure the stdafx.h files are properly inserted in the code. The build order is different so you may get trapped.
Include (#include) the native stdafx.h in the managed pch.h file.
Not all the relevant settings are in the project properties dialog. Also check the properties non-modal window, particularly with the project-to-project reference selected inside Solution Explorer.
There you will find a setting named "Use Library Dependency Inputs" which causes the main project to include the individual source files from the library project, instead of the static library. Make sure this is set to False.

Visual C++ Express 2010 suddenly won't accept #includes

I'm working with an API which has #defineed all their include files. I'm developing in Visual C++ 2010 Express, and it's been working fine up till now.
I was adding a new cpp-file to the project, and accidentally added a "Windows Form" instead. VC warned me that my project was not using CLR at the moment, did I really want to? I clicked no, and added the file as intended. After that, however, my project no longer compiles.
The code looks basically like this:
api_header.h:
#define DEFINED_HEADER_NAME "path/to/header/file.h"
stdhpf.h:
#include DEFINED_HEADER_NAME
As I said, worked fine for a long time. Now I get this:
error C2006: '#include' : expected a filename, found 'identifier'
fatal error C1083: Cannot open include file: '': No such file or directory
What is causing this? I found some post that said it was because of having turned on precompiled headers, but I checked Project properties > Configuration properties > C/C++ / Precompiled headers, and it's off (I mention the setting path since I'm new to VS, there might be more than one way to do it...).
Any ideas?
The problem almost certainly lies in the order in which the two statements are pre-processed, rather than having anything to do with inadvertently adding a Windows Form object.
This knowledge base article suggests:
The problem is in using a defined constant to specify an include file in the #include directive. The directive is being processed before the macro is completely expanded, resulting in the error.
The second error seems to confirm this, as it indicates the pre-processor is searching for an include file with an empty name:
fatal error C1083: Cannot open include file: '': No such file or directory
The order of your include files has changed. Perhaps Visual Studio inserted a #include "stdhpf.h" somewhere ahead of your #include "api_header.h".
Disable precompiled headers. It should helps.

Visual Studio 2008, error c2039: 'set_new_handler' : is not a member of 'std'

So the other day I went to compile a VC++ project I am working on and all of a sudden I get errors in almost all of my files saying:
new.h: error C2039: 'set_new_handler' : is not a member of 'std
new.h: error C2039: 'set_new_handelr' : symbol cannot be used in a using-declaration
"new.h" and 'set_new_handler' are not being used in any of my files, so I have no idea how or why these errors are suddenly appearing since they relate to a windows/VS library file.
Would anyone know what I can do to clear this error and compile my code again?
UPDATE After examining the files being included upon compilation, some files are including and some are . The problem is that is being included in afxwin.h and is being included in a third-party library. I honestly have no idea what to do with this problem...no other developers that have played with this code are running into this problem, may it be a settings problem? I am not using precompiled headers.
If I were to hazard a guess, I would say that <new.h> declares set_new_handler in the global namespace and <new> declares it within the std namespace. Some code is including <new.h> and expecting it to act as if it had included <new>. I would suspect either some 3rd party library/header or a precompiled header as suggested by Evan.
You can narrow down the culprit using either /showIncludes or pre-processing a source code file (using /E) and examining the output. I usually use the latter and look at the #line directives in the output file to figure out the include chain.
Good luck.