场景
如果我的 VS 项目中有以下目录结构。
project/
|
|-- include/
| |
| pch.h
|
|-- src/
| |
| pch.cpp
|
Project.cpp
我的文件是这样的:
Project.cpp
#include "include/pch.h"
int main()
{
std::cout << "Hello World!\n";
}
pch.h
#ifndef _PCH_H
#define _PCH_H
// IO Libraries
#include <iostream>
#include <iomanip>
#include <io.h>
#include <ostream>
#include <conio.h>
... more headers
#endif // !_PCH_H
pch.cpp
#include "../include/pch.h"
现在,为预编译 header 配置每个元素的属性,如下所示:
Project
Precompiled Header Use(/Yu)
Precompiled Header File pch.h
Precompiled Header Output File $(IntDir)$(TargetName).pch
pch.cpp
Precompiled Header Create(/Yc)
Precompiled Header File pch.h
Precompiled Header Output File $(IntDir)$(TargetName).pch
问题
当我尝试编译此项目时,收到以下错误:
Severity Code Description
-------------------------------------------------------------------------------------------------------
Error C2857 '#include' statement specified with the /Ycpch.h command-line option was not found in the source file
Project File Line
-------------------------------------------------------------------------------------------------------
C:\Workspace\VisualStudio\C++\Poker\Poker\src\pch.cpp 2
最佳答案
When you use the /Ycfilename option on a source file to create a precompiled header (PCH) file, that source file must include the filename header file. Every file included by the source file, up to and including the specified filename, is included in the PCH file. In other source files compiled by using the /Yufilename option to use the PCH file, an include of filename must be the first non-comment line in the file. The compiler ignores anything in the source file before this include.
如果这些细节有任何错误,你都会遇到编译错误。在我看来,您为/Yc 选择的源文件实际上并未#include“pch.h”。
关于c++ - VisualStudio 中 C++ 项目中的预编译 header 未正确链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58964711/