Update:
What are the effects of including stdafx.h in my header files?
I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows.
In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h.
Here's my stdafx.h
#pragma once
#include <string>
#include <vector>
#include <map>
...
and my stdafx.cpp
#include "stdafx.h"
In every .h and .cpp file, I have the following:
#pragma once //if in a header file
#include "stdafx.h"
For both release and debug, I have "Create Precompiled Header (/Yc)". It compiled fine in debug mode, but in release mode it keeps reporting
error LNK2005: ___##_PchSym_#00#UfhvihUaszlaDUwlxfnvmghUnnlUhixUnnlPeDUnnlPeDUivovzhvUvmgrgbOlyq# already defined in A.obj
If I switch both to "Use precompiled header", I get in both Debug and Release
fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object file:
Does anyone know what's going on?
You put "create precompiled header" only for stdafx.cpp. Then "use precompiled header" for all of the other ".cpp" files. Finally, have include "stdafx.h" at the start of each ".cpp" file (not usually in the header files.
The /Yc compiler option is used to create a pre-compiled header for a compilation action. The /Yu option instructs the compiler to use a pre-compiled header.
You will always use the /Yu option in project settings.
In the property pages for your stdafx.cpp file, the /Yc option will be set.
It is important to understand that there are separate compilation options for each .cpp file
.
See here for details of the /Y options.
You put the #pragma once before the #include "stdafx.h" which I think is causing the compiler to ignore the #pragma once directive.
Also, I don't think you should be putting the #include "stdafx.h" line into the header files at all.
The results of using "stdafx.h" are not influenced by the PreCompiled Header system. If you turn off Create PCH/Use PCH, the code compiles and creates the same output, except it does so slower. This is also why you can use it in portable code (unlike #pragma once)
Related
#include "pch.h"
#include <stdio.h>
#include <string.h>
What is "pch.h"? Why is it needed to be included as the first header file?
pch stands for precompiled header.
In computer programming, a precompiled header is a (C or C++) header file that is compiled into an intermediate form that is faster to process for the compiler. Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units.
To reduce compilation times, some compilers allow header files to be compiled into a form that is faster for the compiler to process. This intermediate form is known as a precompiled header, and is commonly held in a file named with the extension .pch or similar, such as .gch under the GNU Compiler Collection.
In Visual Studio, precompiled header is usually named "pch.h" (for console based applications), but it is possible to use different name, or not use it at all. Which file would be precompiled header, if any, is determined by projects settings.
If the precompiled header file is "pch.h" and the compile option is /Yu, Visual Studio will not compile anything before the #include "pch.h" in the source file; it assumes all code in the source up to and including that line is already compiled.
I am writing a code for console application in using Visual studio 2010 on windows 7. I have included the libraries used in this but when I am building the program with header files only it is unable to include "stdafx.h" header file. This is my code.
#include "stdafx.h"
#include "stgetriggersample.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
I am getting this error:
error C1189: #error : "include 'stdafx.h' before including this file for PCH"
"stdafx.h" header file is present in my current directory.
Update
The content of "stgetriggersample.h" is
// StGETriggerSample.h : main header file for the PROJECT_NAME application
//
//#include"stdafx.h"
#pragma once
#ifndef __AFXWIN_H__
#include"stdafx.h"
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
// CStGETriggerSampleApp:
// See StGETriggerSample.cpp for the implementation of this class
//
class CStGETriggerSampleApp : public CWinApp
{
public:
CStGETriggerSampleApp();
// Overrides
public:
virtual BOOL InitInstance();
// Implementation
DECLARE_MESSAGE_MAP()
};
extern CStGETriggerSampleApp theApp;
The content of "stdafx.h" is:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
I tried writing the code with the option Not Using Precompiled Headers but still I get the same error.
How this can be corrected. Thanks
Perhaps you have been trying to fix this so hard, that, in fact, you have broken something. Let's go through proper configuration of pre-compiled headers usage in Visual Studio (I will use options names from VS2012):
Enable usage of PCH: Project Properties -> Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header -> Use (/Yu).
Set name of precompiled header: Project Properties -> Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header File. In your case you should see there standard PCH name (stdafx.h). You may change that if you want.
(In fact, the most important step) Enable PCH generation: Header is not enough. You also need a .cpp file, that is responsible for PCH content generation (since only source files can be compiled). In you case, you have default stdafx.cpp attached to your project. Click it with RMB, then: Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header -> Create (/Yc).
From now on, first non-empty line of every source file attached to your project (including the one from step 3, responsible for PCH generation) should look like this:
#include "PCH_NAME"
Where PCH_NAME is the name of precompiled header set in step 2.
After all these settings are set this way, I would suggest you to:
Clean your solution.
Check in project properties paths of Output Directory and Intermediate Directory. Delete them (I hope they don't contain any code - they shouldn't).
In solution folder (*), you should also see folder with name like: your solution name-cc50b4e6. Delete it.
This will make full reset of your solution (including IntelliSense database etc.). Then, open your project and compile it.
(*) - In your solution folder, by default. If you have valid Fallback Location set, it will be placed there.
In the code:
#ifndef __AFXWIN_H__
#include"stdafx.h"
#error "include 'stdafx.h' before including this file for PCH"
#endif
it looks like StGETriggerSample.h is checking that stdafx.h has been #included by looking for the compile guard macro AFXWIN_H__. However, stdafx.h is not actually defining this guard macro, it's just relying on the statment "#pragma once" to avoid multiple inclusions. So even if stdafx.h has been included, the #error statement will always trigger.
To fix, add the compile guard
#ifndef __STDAFX_H__
#define __STDAFX_H__
// ... yadda yadda yadda...
#endif // __STDAFX_H__
to your stdafx.h. You can remove the #pragma once statement from stdafx.h if you want to, but it's not necessary.
As an aside, your code is also attempting to #include stdafx.h if it hasn't already been included, but then causing the compile error anyways. I'm pretty sure you only want to do one or the other...
I have a precompiled header that needs to be included atop each .cpp in my project. I fear that I will waste a lot of time explaining and fixing that in coworkers code.
Is there an MSBuild step that I can do to #include "stdafx.h" at the top of all my .cpp files so it dosen't need to do done manually?
You have the compiler option /FI pathname
which virtually add #include "pathname" at the first line of the file.
Note: the equivalent for gcc is -include path.
I'm working on a VC++ project in VS 2012 that takes about 8-10 minutes for a full compile. I know PCH can speedup compile times by upto 10x. I have currently disabled PCH in my project and I'm including header files where they are needed. How do I get started with PCH? I've looked everywhere for "how to" guides but all I got is the docs.
I'm assuming I'll have to :
Configure my project for PCH, creating a blank PCH header file
Collect headers from every .cpp file and place it into the PCH header file
Modify every file removing all header imports
Recompile and hope that nothing goes wrong ;)
How do I get started with this (specifically #1)? Have you modified a project to use PCH and what are the stumbling blocks or common problems/issues therein? Can PCH cause any problems or is it just the same compile-time/runtime behaviour as normal includes? Is there a tool to automate the process or do I have to go thru 500 .cpp files by hand and modify it to use PCH?
And last but not least, what is the compilation time speedup I can expect with PCH? Is it 2x-10x? Or would it just go like 30% faster? (which does not justify the time involved)
After configuring my project to use PCH, full-compile times were down to half, and incremental builds occurred almost instantly. PCH is a very effective way to speedup compile times, and I highly recommend it.
Althouh dsharlet mentions many important points, he skips some crucial steps that I had to eventually figure out. So here is the complete guide to configuring your project to use PCH:
Getting started with PCH in a VC++ project
Backup your src dir and any other directories that contain source code ... (you'll need this in case anything goes wrong)
Create 2 files in your project, Globals.cpp and Globals.h .. (choose any name but stick to it)
Right click Globals.cpp and open Properties, choose Configuration > All configurations
Go to C/C++ | Precompiled Header, and fill these in:
Precompiled Header : Create (/Yc)
Precompiled Header File : Globals.h
Open Globals.cpp and add this one line in, and nothing more: #include "Globals.h"
Right click your VC++ project and open Properties, choose Configuration > All configurations
Go to C/C++ | Precompiled Header, and fill these in:
Precompiled Header : Use (/Yu)
Precompiled Header File : Globals.h
Open all the .h and .cpp files in your project, and add this at the very top: #include "Globals.h". If you DONOT want to include every file manually, you can use the Force Include /FI[name].
Open Globals.h and add the following in: (its very important you have #pragma once at the top)
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <memory>
#include <string.h>
#include <limits.h>
#include <float.h>
#include <time.h>
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include <malloc.h>
#include <locale.h>
#include <math.h>
// Windows SDK
#define _WIN32_WINNT 0x0501 // _WIN32_WINNT_WINXP
#include <SDKDDKVer.h>
// Windows API
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
These includes are typical candidates for your PCH file
Remove the includes that you're not using
Go through your project and collect any more header files that do not change often
Using find and replace, search for each of the #include's in your PCH file, and remove them from all the .h and .cpp files in your project.
Do a full compile and ensure everything is working okay. Here are some solutions for common problems you'll encounter:
PCH file includes itself:
Your PCH file is including a header that includes the PCH header file again, creating a kind of circular dependency. Double click the error to take you to the offending file, and simply remove the line that says #include "Globals.h"
Undefined symbol X
Although all your project files can include the PCH header, the files included inside the PCH header cannot include the PCH header! (as stated above) so you'll need to add back any imports that were previously in the file. (diff the file with the backup version)
Cannot find symbol logf
Sometimes the global PCH file does not behave as expected, and breaks compiling with crazy errors that are impossible to solve. You can then turn off PCH for individual source code files.
Right click your .cpp file and open Properties, choose Configuration > All configurations
Go to C/C++ | Precompiled Header, and fill these in:
Precompiled Header : Not Using Precompiled Headers
Remove the line #include "Globals.h" in your .cpp file
Add back whatever imports the file originally had. (diff the file with the backup version)
Here's how I use PCH with decent results:
Go to the project properties, C/C++|PCH set Precompiled Header option to 'Use'. Set Precompiled Header File to something you want.
Go to the properties of a cpp file you want to be the PCH, and set the Precompiled Header option to 'Create' (it will have defaulted to 'Use' from the project property setting).
Include the pch header in all your cpp files in the project (basically, the ones that have 'Use' set for the Precompiled Header option). I suppose you could turn off 'Use' for some cpp files in the project instead of add the include for the PCH, but I've never tried that...
At this point, the project should still build and behave exactly as it used to, but there may not be any real improvement in compile time. Now, you'll need to move some of your #include "...h" to the PCH header file (and delete the includes of those files from elsewhere in the project). The includes that you should move to the PCH header should be headers that are included in many files, but change infrequently. Examples: STL headers, windows.h, core functionality headers from your project, etc.
Once PCH is set up, it should be transparent. It's basically just helping the compiler cache some intermediate compilation data. In other words, if you turned off PCH in your project, everything should still build exactly as it would have with PCH turned on (except slower!)
The speedup entirely depends on how much code is moved into the PCH (how much included code from headers is moved from arbitrary cpp files to the PCH header). I've seen multiple times improvement, but haven't benchmarked it precisely. I definitely felt like it was worth doing when I've gone through the trouble to use PCH on a big project.
We do have a project wich uses the MIDL tool to create specific header/iid and proxy files. Those files are compiled and linked with the rest of the project using a post build step that calls nmake.
Is it possible to use precompiled headers with thos IDL generated files? How can I inject #include "stdafx-h" and remove other included headers?
Use the /FI option (Force Include): "This option has the same effect as specifying the file with double quotation marks in an #include directive on the first line of every source file specified on the command line, in the CL environment variable, or in a command file."
It won't remove the other headers, but this is not necessary for the Precompiled Header to be used... All the headers that you want to precompile should be included by stdafx.h. Then, provided the files have inclusion guards, it won't be a problem when they are included again in the sources.
Example
Generated a.cpp file:
#include <a.h>
#include <b.h>
//rest of the code
Suppose you want to pre-compile a.h and b.h. Then you create the file stdafx.h:
#include <a.h>
#include <b.h>
And then you use the /FI option to have this stdafx.h included as the first file into a.cpp. If the files a.h and b.h have include guards, leaving them in a.cpp is not an issue...
"stdafx.h" is merely a convention. If you know that yout generated source files always have a standard prefix of included headers, you can name the last of them in the /Yu switch (use precompiled headers). To create the PCH, create an single .cpp file with just those fixed headers and compile ith with /Yc.