在 Visual C++ 中,force include 可以与 pre-compiled headers 一起使用吗?

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

如果为单个 cpp 文件设置了 /Yu(即不是为整个项目),是否可以设置 /FI 以指定要包含的 header 文件或必须header 文件包含在例如 #include "stdafx.h" 中,如果 /Yu"stdafx.h" 传递给 CL?

以下所有结果基本相同的错误...

header 在 C:\path\to\stdafx.htest.cpp 就像这样...

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

其中任何一个要编译...

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?

我觉得应该可以用/FI指定预编译的header.

这个答案似乎表明它甚至是首选方法

问题是 stdafx.h 不是 C:\path\to\stdafx.h。 VC++ 进行字符串比较。您需要将 C:\path\to\ 添加到包含路径,这样您就可以只使用 /FI"stdafx.h"

经过一些实验,我设法让它正常工作。结果我所要做的就是提供 /Yu

相同的值
/FI"stdafx.h"