Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
PRB: C1853 Error Generated by Mixing C and C++ Files
Article ID: 126717 - View products that this article applies to.
This article was previously published under Q126717
When you build Visual C++ projects that include both C and C++ files and that use precompiled headers, you may receive the following error message:
fatal error C1853: '<filename>' is not a precompiled header file
created with this compiler
The compiler attempts to use a precompiled header generated by a C++ compiler on a C source file. This can happen when "Per-File Use of Precompiled Headers" is used with both C and C++ source code in a project.
It is not valid to use a C++ compiler created precompiled header with a source file that is to be compiled by the C compiler. Nor is it valid to use a C compiler created precompiled header with a source file that is to be compiled by the C++ compiler.
The Visual C++ version 2.x error message help for C1853 may be misleading. It implies that the problem occurs only when mixing .PCH files generated with the 16-bit and 32-bit compilers. Frequently, however, this can occur when building a project after adding a C source file to a pure C++ project that uses the "Per-File Use of Precompiled Headers." (The Visual C++ version 4.0 online help correctly incorporates this information by stating that fatal error C1853 "can be caused by using a precompiled C++ file with C code.")
This problem can be resolved in several ways depending on the situation:
Resolution IFor projects with a single C source file and multiple C++ source files (or vice-versa), use the following steps to disable precompiled headers for the single source file:
Resolution IIFor projects with multiple C and C++ source files, use the following steps to disable precompiled headers for a set of source files procedure:
With Visual C++ version 2.x:
With Visual C++ version 4.0, groups are no longer supported. To disable the use of precompiled headers for a group of source files, follow the the steps of Resolution I, but in step 2 hold down the CRTL key while selecting each file.
Resolution IIIFor projects not requiring precompiled headers, follow the steps of Resolution I, but in step 2 select all targets from the project tree in the left pane.
Resolution IV (VC 4.x and 5.0)
Visual C++ version 2.0 allows only one precompiled header file per target in a project. This file is named PROJNAME.PCH by default where PROJNAME is the name of the project. This can be changed in the Project Options edit control of the target settings using the /Fp option (for example, "/Fp MYNAME.PCH").
This precompiled header (.PCH) file can be generated using either the Automatic or Per-File Use system as shown in the Precompiled Headers section of the C/C++ tab dialog project settings.
The Automatic PCH option (/YX option) generates a .PCH file when the current .PCH file is invalid or when the header file is updated. Invalid .PCH files include those generated by another compiler (C or C++) or those that contain a different set of header files from the set found in the source. This option optimally saves compilation time if all source files that use this option contain an identical set of header files and are of one type (C or C++), or if only one file in the project is usually built.
The efficiency of this option can depend on compilation order. Interleaving C and C++ source files in the compilation order always regenerates a new .PCH file. Building a project after updating a C file when the last build resulted in a C++ .PCH file, regenerates the .PCH file.
The Per-File Use PCH option generates a .PCH file whenever a source file with the 'Create .PCH File' (/Yc option) checkbox enabled is compiled. The generated .PCH file is used with all source files that have the 'Use .PCH File' (/Yu option) checkbox enabled. The compiler doesn't regenerate a new .PCH file if the generated one is invalid; instead, it gives one of the following two messages:
Previous versions of Visual C++ (1.51, 1.50, and 1.1) allowed a separate precompiled header file for C and C++ source files. Because Visual C++ version 2.0 is limited to only one PCH, it is not possible to use different precompiled header files for C and C++ files. Porting such a project over will result in the following message when attempting to build the project:
The source files "file1.c" and "file2.cpp" are both configured to
produce the output file "proj.pch". The project cannot be built.
For more information on precompiled headers, please search on "Precompiled Headers" in Books Online.
Article ID: 126717 - Last Review: July 5, 2005 - Revision: 1.1