You are currently offline, waiting for your internet to reconnect

PRB: C1853 Error Generated by Mixing C and C++ Files

This article was previously published under Q126717
SYMPTOMS
When you build Visual C++ projects that include both C and C++ files andthat use precompiled headers, you may receive the following error message:
fatal error C1853: '<filename>' is not a precompiled header file
created with this compiler
CAUSE
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 ofPrecompiled 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 asource file that is to be compiled by the C compiler. Nor is it valid touse a C compiler created precompiled header with a source file that is tobe 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 filesgenerated with the 16-bit and 32-bit compilers. Frequently, however, thiscan occur when building a project after adding a C source file to a pureC++ project that uses the "Per-File Use of Precompiled Headers." (TheVisual C++ version 4.0 online help correctly incorporates this informationby stating that fatal error C1853 "can be caused by using a precompiledC++ file with C code.")
RESOLUTION
This problem can be resolved in several ways depending on the situation:

Resolution I

For projects with a single C source file and multiple C++ source files (orvice-versa), use the following steps to disable precompiled headers for thesingle source file:

  1. For the current target, open the Project Settings dialog box and select the Precompiled Headers section under the C/C++ Tab.
  2. Select the C file from the project tree in the left pane.
  3. For Visual C++ version 2.x, disable the Use .PCH File checkbox in the Per-File Use of Precompiled Headers. For Visual C++ version 4.0, 5.0, and 6.0, select the Not using precompiled headers option button.
  4. Save the new settings and build the project.

Resolution II

For projects with multiple C and C++ source files, use the following stepsto disable precompiled headers for a set of source files procedure:

With Visual C++ version 2.x:

  1. For the current target, create a New Group by choosing New Group from the Project menu item.
  2. Move the C source files into the new group.
  3. Open the Project Settings and select the Precompiled Headers section under the C/C++ tab dialog.
  4. Select the new group from the project tree in the left pane.
  5. Disable the Use .PCH File checkbox in the Per-File Use of Precompiled Headers.
  6. Save the new settings and build the project.
This will disable precompiled headers for a set of source files. However, source files in subsequent additions to this group may need the PCH usage disabled depending on the file's previous settings.

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 III

For projects not requiring precompiled headers, follow the steps ofResolution I, but in step 2 select all targets from the project tree inthe left pane.

Resolution IV (VC 4.x and 5.0)

  1. Create a subproject of type "Static Library".
  2. Insert the .C files into this subproject.
  3. Delete the .C files from the top-level project.
  4. Add the resulting library or .obj files to the link line for the top-level project.
This will allow you to specify a pre-compiled header for the top-levelproject (.CPP files), and a different pre-compiled header for thesubproject (.C files).
STATUS
This behavior is by design.
MORE INFORMATION
Visual C++ version 2.0 allows only one precompiled header file per targetin a project. This file is named PROJNAME.PCH by default where PROJNAME isthe name of the project. This can be changed in the Project Options editcontrol of the target settings using the /Fp option (for example, "/FpMYNAME.PCH").

This precompiled header (.PCH) file can be generated using either theAutomatic or Per-File Use system as shown in the Precompiled Headerssection of the C/C++ tab dialog project settings.

The Automatic PCH option (/YX option) generates a .PCH file when thecurrent .PCH file is invalid or when the header file is updated. Invalid.PCH files include those generated by another compiler (C or C++) or thosethat contain a different set of header files from the set found in thesource. This option optimally saves compilation time if all source filesthat use this option contain an identical set of header files and are ofone 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. InterleavingC 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 buildresulted in a C++ .PCH file, regenerates the .PCH file.

The Per-File Use PCH option generates a .PCH file whenever a source filewith the 'Create .PCH File' (/Yc option) checkbox enabled is compiled. Thegenerated .PCH file is used with all source files that have the 'Use .PCHFile' (/Yu option) checkbox enabled. The compiler doesn't regenerate a new.PCH file if the generated one is invalid; instead, it gives one of thefollowing two messages:
  • If the PCH was generated by the C++ compiler:
    fatal error C1853: '<filename>' is not a precompiled header file created with this compiler
  • If the PCH was generated by a source code containing a different set of includes:
    fatal error C1852: '<filename>' is not a valid precompiled header file
NOTE: It is possible to specify more than one source to create a .PCHfile, however Microsoft does not recommend it. Nor does Microsoft recommendthat you mix the two PCH systems because mixing the two can result in theerrors outlined above.

Previous versions of Visual C++ (1.51, 1.50, and 1.1) allowed a separateprecompiled 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 differentprecompiled header files for C and C++ files. Porting such a project overwill 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.
REFERENCES
For more information on precompiled headers, please search on "PrecompiledHeaders" in Books Online.
9.00
Properties

Article ID: 126717 - Last Review: 07/05/2005 22:38:00 - Revision: 1.1

  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • kberrmsg kbprb kbcpponly kbcompiler KB126717
Feedback