Justin Byrne
Justin Byrne

Reputation: 341

Precompiled Header within C++ Project in VisualStudio not Linking correctly

Scenario

If have the following directory structure within my VS Project.

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

And my files read like this:

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"

Now, the properties for each element are configured for Precompiled Headers as follows:

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

Problem

When I attempt to compile this project, I receive the following error:

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

Upvotes: 3

Views: 1062

Answers (1)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

According to the 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.

Get any of those details wrong and you'll be slapped with a compile error. Looks to me like the source file you selected for /Yc does not in fact #include "pch.h".

Upvotes: 4

Related Questions