Search code examples
c++visual-c++precompiled-headers

Can force include be used with pre-compiled headers in Visual C++?


If /Yu is set for an individual cpp file (i.e. not for an entire project) is it possible to set /FI to specify the header file to be included or must the header file be included with for example #include "stdafx.h" if /Yu"stdafx.h" is passed to CL?

All the following result in basically the same error...

With a header at C:\path\to\stdafx.h and test.cpp like so...

// Not including `stdafx.h`
void foo() {}

And any one of these to compile...

CL.exe /c /Yu"stdafx.h" /FI"C:\path\to\stdafx.h" test.cpp
CL.exe /c /Yu /FI"C:\path\to\stdafx.h" test.cpp
CL.exe /c /Yustdafx.h /FIC:\path\to\stdafx.h test.cpp
CL.exe /c /Yu /FIC:\path\to\stdafx.h test.cpp

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

It seems to me that it should be possible to use /FI to specify the precompiled header.

This answer seems to suggest it is even the preferred method https://stackoverflow.com/a/11052390/6225135


Solution

  • The problem is that stdafx.h is not C:\path\to\stdafx.h. VC++ does a string compare. You'd need to add C:\path\to\ to the include path, so you can use just /FI"stdafx.h"