c++ - VisualStudio 中 C++ 项目中的预编译 header 未正确链接

标签 c++ visual-studio precompiled-headers

场景

如果我的 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

最佳答案

根据Compiler Error C2857

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/

相关文章:

C++ linux 监控 sigsegv 进程

c# - 从托管代码调用时 C++ DLL 崩溃

visual-studio - 用于色彩空间转换的 IMTransform 视频处理器的设置

.net - Visual Studio 是否可以像使用 app.config 一样自动调整其他文件的名称?

c++ - 在预编译单元中定义宏条件语句是否明智?

c++ - 包含标准库时不使用预编译头文件

c++ - 当所有值都是非负数时,我们真的可以避免额外的空间吗?

c++ - 当小部件看不见时的 Qt 事件

visual-studio - vs 2008 Express : registration

c++ - 在预编译头文件中实例化模板会减少编译时间吗?