Answered by:
How Set the /YX automatic Pre-compiled Header Option?

Question
-
Hello,
I'd like to use the "automatic management of pre-compiled headers" option for my VC++ Solution. However, there is no such option available in the Configuration Property Sheet. I can select "Not Using", "Create" and "Use" but there are no other options. If I manually add /YX to the Command Line | Additional Arguments, this is ignored.
How can I get this option?
Thanks.
Friday, March 11, 2011 5:09 AM
Answers
-
Hello,
Precompiled header file is a performance feature which is used to compile a stable body of code, store the compiled state of the code in a file, and, during subsequent compilations, combine the precompiled code with code that is still under development. Each subsequent compilation is faster because the stable code does not need to be recompiled. Therefore, we just put stable code and headers into precompiled header.
For your first question, if we don’t change anything inside stdafx.cpp or stdafx.h, .pch is stable, stdafx.cpp will not be recompiled and .pch file will not be recreated. As we can see, this is how precompiled header improve the performance.
For your second question, you don’t need to worry about manually updating the pch file. The compiler will check this for you to make sure all files are up to date.
If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.
Regards,
Yi
Yi Feng Li [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by MS Cpp User Wednesday, March 16, 2011 3:17 PM
Wednesday, March 16, 2011 3:50 AM
All replies
-
Hi
According to your description, you mean you cannot find the "automatic management of pre-compiled headers" option. Could you tell which Visual Studio version you have use.
Since Visual Studio 2005, /YX compiler option is removed. You can find detail information in this site,http://msdn.microsoft.com/en-us/library/ms177253(v=vs.80).aspx.
If you use the version before Visual Studio 2005 like Visual Studio 2003, you could follow the step in the site, http://msdn.microsoft.com/en-us/library/ms177253(v=vs.80).aspx
Best Regards
Kelvin
- Proposed as answer by Yi Feng Li Monday, March 14, 2011 5:16 AM
Friday, March 11, 2011 9:45 AM -
Hi Kelvin,
I am using VS 2010 so I guess /YX is no longer available. That means I need to use /Yc (Create) and Yu (Use). Even though I have read http://msdn.microsoft.com/en-us/library/szfdksca%28v=vs.80%29.aspx I'm not certain I understand how to use these. Can you tell me if the following is correct:
1) I have a StdAfx.h in which I have #included a collection of C++ .h header files that are used by all of the modules in a dll and that do not change often.
2) In each .cpp file (including StdAfx.cpp) of the dll I have:
#include StdAfx.h
#pragma hdrstop
#include ...
3) I have set the dll's Project Configuration as:
Precompiled Header (Use) /Yu
Precompiled Header File StdAfx.h
Precompiled Header Output File MyLib.pch
4) I have set StdAfx.cpp's File Configuration as:
Precompiled Header (Create) /Yc
Precompiled Header File StdAfx.h
Precompiled Header Output File MyLib.pch
Does this mean that:
1) StdAfx.cpp is compiled 1st and when it is compiled the MyLib.pch file containing the precompiled StdAfx.h is created?
2) Then all the other .cpp files are compiled and processing skips over the StdAfx.h in each .cpp and instead uses the MyLib.pch?
If the above is correct I guess I've figured this out. If not, I'm really confused. Can you verify the above or tell me where I'm wrong?
Thanks!Monday, March 14, 2011 9:07 PM -
Hello,
Based on my knowledge, your understanding is correct. One thing you need to notice is in property pages we have defined the Precompiled header filename and the output PCH file, in this case, you are not necessary to call #pragma hdrstop.
When you specify the /Yu option without a file name, your source program must contain a #pragma hdrstop pragma that specifies the file name of the precompiled header, .pch file. In this case, the compiler will use the precompiled header (.pch file) named by /Fp (Name .pch File). The compiler skips to the location of that pragma, restores the compiled state from the precompiled header file specified by the pragma, and then compiles only code that follows the pragma. If #pragma hdrstop does not specify a file name, the compiler looks for a file with a name derived from the base name of the source file with a .pch extension. You can also use the /Fp option to specify a different .pch file. It is similar for /Yc.
For more information, please check http://msdn.microsoft.com/en-us/library/z0atkd6c.aspx (/Yu)
And http://msdn.microsoft.com/en-us/library/7zc28563.aspx (/Yc).
Hope the information helps.
Regards,
Yi
Yi Feng Li [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Tuesday, March 15, 2011 3:33 AM -
Hi Yi,
Thanks for confirming that my understanding is correct. I have a couple of follow on questions:
Say that the project has been built once using the configuration I described above. That means that a .pch was built when StdAfx.cpp was compiled and that .pch was then used instead of StadAfx.h when all the other .cpp files were built in the dll's Project. Now let's say that a change is made to one of the .cpp files and I rebuild the Project. Only the changed .cpp file should be recompiled. Is it correct that the .pch is not rebuilt, making the .cpp build go very quickly. Or, is the .pch always rebuilt when any .cpp in the Project is changed?
If the answer to my 1st question is that the .pch is not rebuilt, what happens when a change is made to a .h header that was in StdAfx.h which was used the create the .pch? I realize that only .h files that don't change often should be included in StdAfx.h but sometimes a change needs to be made. Do I have to "manually" force the .pch to be rebuilt and if so how? Or, does the compiler always check to see if the .pch needs to be rebuilt so that I don't have to worry about changes made to .h files in StdAfx.h?
Thanks.
Tuesday, March 15, 2011 4:38 PM -
Hello,
Precompiled header file is a performance feature which is used to compile a stable body of code, store the compiled state of the code in a file, and, during subsequent compilations, combine the precompiled code with code that is still under development. Each subsequent compilation is faster because the stable code does not need to be recompiled. Therefore, we just put stable code and headers into precompiled header.
For your first question, if we don’t change anything inside stdafx.cpp or stdafx.h, .pch is stable, stdafx.cpp will not be recompiled and .pch file will not be recreated. As we can see, this is how precompiled header improve the performance.
For your second question, you don’t need to worry about manually updating the pch file. The compiler will check this for you to make sure all files are up to date.
If you need further assistance, feel free to let me know. I will be more than happy to be of assistance.
Regards,
Yi
Yi Feng Li [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by MS Cpp User Wednesday, March 16, 2011 3:17 PM
Wednesday, March 16, 2011 3:50 AM -
Hi Yi,
Thanks to you and Kelvin for your help with this!
Wednesday, March 16, 2011 3:18 PM -
All these answers aren't totally exact.
If the option /XY has been removed there is another way to add an automatic management of pre-compiled headers.
It's the 'Force Include' option in 'C/C++/Advanced' tab. You just have to put the name of pre-compiled header (ususally stdafx.h or stable.h). This add /FI stdafx.h in the command line for the compiler and by the way stdafx.h will be included at the beginning of each file compiled. By this way no source file has to be modified for using pre-compiled headers
Thursday, August 23, 2018 4:21 PM