gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:49:31 25 4
gpt4 key购买 nike

情境

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

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

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++ - Visual Studio中的C++项目中的预编译头无法正确链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58964711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com