【问题标题】:Precompiled Header within C++ Project in VisualStudio not Linking correctlyVisualStudio 中 C++ 项目中的预编译标头未正确链接
【发布时间】:2020-03-16 18:47:14
【问题描述】:

场景

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

    project/
      |
      |-- include/
      |     |
      |     pch.h
      |
      |-- src/
      |     |
      |     pch.cpp
      |
      Project.cpp

我的文件是这样的:

项目.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】:

    根据Compiler Error C2857

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

    如果这些细节中有任何错误,您都会遇到编译错误。在我看来,您为 /Yc 选择的源文件实际上并不 #include "pch.h"。

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 2012-12-02
      • 1970-01-01
      • 2021-10-22
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      相关资源
      最近更新 更多