Visual Studio中的C ++项目中的预编译头无法正确链接

时间:2019-11-20 22:58:23

标签: c++ visual-studio precompiled-headers

场景

如果在我的VS Project中具有以下目录结构。

    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"

现在,为预编译头文件配置了每个元素的属性,如下所示:

  

项目

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

1 个答案:

答案 0 :(得分:2)

根据Compiler Error C2857

  

当您在源文件上使用/ Ycfilename选项创建一个   预编译头(PCH)文件,该源文件必须包含   文件名头文件。源文件包含的每个文件,最多   并且包括指定的文件名,包含在PCH文件中。在   使用/ Yufilename选项编译的其他源文件使用   PCH文件,文件名的包含必须是其中的第一条非注释行   文件。在此之前,编译器将忽略源文件中的任何内容   包含。

弄错这些详细信息中的任何一个,您就会被编译错误打败。在我看来,您为/ Yc选择的源文件实际上并没有#include“ pch.h”。