Edit

Share via


Compiler Error C2857

'#include' statement specified with the /Ycfilename command-line option was not found in the source file

The /Yc option specifies the name of an include file that is not included in the source file being compiled.

Remarks

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.

This error can be caused by an #include "filename" statement in a conditional compilation block that is not compiled in your PCH source file.

Example

In typical usage, one source file in your project is designated as the PCH source file, and one header file is used as the PCH header file. A typical PCH header file has all of the library headers used in your project, but not local headers that are still under development. In this sample, the PCH header file is named my_pch.h.

// my_pch.h
#pragma once
#include <stdio.h>

The PCH source file is compiled by using the /Ycmy_pch.h option. If the compiler does not find an include of this PCH header file, it generates C2857:

// my_pch.cpp
// Compile by using: cl /EHsc /W4 /Yumy_pch.h /c my_pch.cpp

#if 0
#include "my_pch.h"  // C2857; remove conditional directives to fix
#endif

To use this PCH file, source files must be compiled by using the /Yumy_pch.h option. The PCH header file must be included first in source files that use the PCH:

// C2857.cpp
// Compile my_pch.cpp first, then
// compile by using: cl /EHsc /W4 /Yumy_pch.h my_project.cpp my_pch.obj
// Include the pch header before any other non-comment line
#include "my_pch.h"

int main()
{
    puts("Using a precompiled header file.\n");
}