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
Related
While creating a dll project in VS17, I see multiple files were created on initialization.
But whichever project on C++ I work on, I don't see any such files in their environment. How can I get rid of these files in my environment. Is there any workaround to remove them entirely or reduce these 4 files to one file to reduce mess?
Also before VS17 we used to have stdafx.h, it is mandatory to include this header but in few projects I couldn't find this file, is there any way to remove these initial files entirely?
When you create a new project in Visual Studio, a precompiled header file named pch.h is added to the project. (In Visual Studio 2017 and earlier, the file was called stdafx.h.)
For more details about pch.h and stdafx.h, I suggest you could refer to the Doc:
Precompiled Header Files
If you couldn't want to use precompiled header file, I suggest you could choose to not use precompiled headers (Configuration Properties > C/C++ > Precompiled Headers).
When you create a dll project, you also directly choose uncheck Precompiled header or check Empty project.
stdafx.h/cpp resp. pch.h/cpp are the default generated files for supporting precompiled headers. If you don't want them you can delete them. In order to compile you then have to go to your project settings Configuration Properties -> C/C++ precompiled headers and change the Precompiled Header setting to Not using Precompiled Headers.
Before you delete thos files, you should check the cpp/h file and put the include files into your files.
I know there are a few topics about it but I haven't found the answer to my question.
So my question is in Visual Studio 2017, everytime I create a project it's created with the class pch.h and also creates pch.cpp.
I would like to change the name of pch.h to a more pertinent name but it seems to always be looking for pch.h even though I have modified all the #include references.
Thanks in advance!
Adjust the name of the precompiled header file in the Project Properties under Configuration Properties -> C/C++ -> Precompiled Headers.
Note that pch.cpp has property to generate precompiled header.
Basically you may set the properties if and what precompiled header you use differently for each file in your project.
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
As the title says. I have a C++ project, and added 1 C .h file and a C .c source file, but they are having problems because the rest of the project uses a precompiled header, but if I try to include it in those C files, it gives an error. I can't just disable precompiled headers for the whole project, because it's part of a larger project that uses it and has code by somebody else.
So is there a way to select those two C files (the .h header and .c source files) and disable PCH?
Thanks.
Highlight the file in the Solution Explorer and hit Alt+F7. This will bring up the properties for the file; in the "C/C++" section under "Precompiled Headers" you'll find the relevant options. You don't need to worry about the .h files.
In C++ at least, you can right click on the cpp file itself and hit properties -- this lets you override C/C++ compiler settings for that file only. You can use that to disable precompiled headers for that one file.
Does every file need to #include "stdafx.h" when using precompiled headers? Or do only source files need to include it.
EDIT: Also, my precompiled header file #includes a lot of STL headers. But, in my headers, I sometimes have functions that return std::vector or something like that, so I need to #include <vector> anyway. Is this worse than including stdafx.h? I need to include the definitions for my unit testing framework.
Every source file needs to include it before any non-comment line. Headers do not need to include it, as every source file will include it before any other header.
You can set whether you want to use a precompiled header file or not at the project level or a file level. For the project setting, go to project properties dialog and click Configuration Properties / C/C++ / Precompiled Headers. There is an option for Create/Use Precompiled Header. You can overwrite the project level setting on each .cpp file by setting the same option on file properties dialog.
All you C/C++ files need to have your precompiled header at the top yes - if it set for that project. But you can have it turned off/on per C/C++ file (what Kei said).
If you have standard headers to include, place them all in the precompiled header. Then you want need them in the normal header (.h) files. As long as all projects and files are using precompiled headers.
As other have said, only your source files need to include the precompiled header. I would only add that, in my opinion, code should compile correctly with or without precompiled headers. To achieve this, headers should be self contained and not rely on the source files including other required headers. I see precompiled header only as a way to speed up the compilation and not as a repository of all used headers in a project.