Tuesday, 12 August 2014

Set a single file to not use precompiled header

If you are working on a C++ project, occasionally you will come across one or more files that cannot include the precompiled header (often named stdafx.h). However when removing the include for this file you will get an error similar to the following:

"unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

Don't worry we don't have to resort to removing the precompiled header entirely. Doing so would often largely increase the compile time and in fact break compilation under certain conditions. Instead the preferred method is to opt an individual file out of the precompiled header.

We can do this by right clicking on the .cpp file under the Solution Explorer and selecting Properties. Next select C++ and then Precompiled Headers in the side menu.

Before we make any changes set the configuration combo select to All Configurations, otherwise any changes we make will only apply to you current configuration i.e. Debug or release and as soon as you switch between them, compilation will fail again.

You should see one of the options listed is Precompiled Header which should be set to Use (/Yu). We need to set it to the Not Using Precompiled Headers option to tell Visual Studio that this .cpp file will no longer be making use of the precompiled header when it is compiled.

Note that the only file that should be set to Create (/Yc) is the precompiled header .cpp file (usually stdafx.cpp). This is the file that visual studio will compile first in order to generate the precompiled header.



After making these changes the above error should be resolved and your code will hopefully compile.

0 comments:

Post a Comment