VisualStudio中C ++项目中的预编译头未正确链接

问题描述 投票:0回答:1

场景

如果在我的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 Headers配置每个元素的属性,如下所示:

项目

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
c++ visual-studio precompiled-headers
1个回答
0
投票

根据Compiler Error C2857

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

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

© www.soinside.com 2019 - 2024. All rights reserved.