25

I am using pre-compiled headers in my project in C but we are integrating a .CPP file into the project.

Here's the error:

Error   1   fatal error C1853: 'Debug\MuffinFactory.pch' precompiled header
file is from a previous version of the compiler, or the precompiled header is C++
 and you are using it from C (or vice versa)    
c:\users\blake\desktop\projects\muffinfactory\source\main.cpp   1

We only need a single .CPP compiled in our project, but we really need the pre-compiled header to save compile times (Windows.h and more).

How should I organize my project to do this?

James Linden
  • 1,055
  • 3
  • 11
  • 21
  • Can you compile your C as C++? –  Jan 19 '12 at 22:39
  • 1
    This is unlikely to be the last problem you run into trying to combine like this. The usual problem is trying to insert a few .C into a C++ project, not the other way around. – Mark Ransom Jan 19 '12 at 22:42
  • stdafx.h for Novices - http://www.viva64.com/en/b/0265/ –  Jul 15 '14 at 07:33

5 Answers5

19

So don't use precompiled headers for that single file!

Being a .cpp file, it will have separate compilation options anyway.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • please explain what you replied @ Persson – Mr. Perfectionist Apr 24 '15 at 13:22
  • 1
    @nafeeur This was a long time ago, but I believe the idea was that C and C++ would likely require different compiler options, so the precompiled header would not be usable for both sets anyway (had the compiler accepted it). So just compile the single CPP file separately, and don't use a precompiled header for it. – Bo Persson Apr 25 '15 at 09:49
6

You might be able to create two precompiled headers in your project. There's a property on each source file that determines if it's going to use a precompiled header, or generate a precompiled header - try setting two different sources to generate a header.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Great option! Requires a little fiddling with the .vcproj though. – James Linden Jan 19 '12 at 23:09
  • 11
    Extra Info: Select .c file you don't want to use "Precompiled Header" from the Solution Explorer -> Right Click -> Under Precompiled Headers Option, Select Not Use Precompiled Header – Zuuum Feb 19 '12 at 18:34
1

Zuuum's Answer

Not using precompiled headers

Apologies to Zuuum for such a blatant rip off of his answer, but 7 years later it is still buried as a comment. Bo Perrson tells us what to do

So don't use precompiled headers for that single file!
It will have separate compilation options anyway.

and Zuuum tells us how

Extra Info: Select the file you don't want to use "Precompiled Header" from the Solution Explorer
Right Click
Under Precompiled Headers Option, Select Not Use Precompiled Header
– Zuuum Feb 19 '12 at 18:34

I've made tiny edits - they are not direct quotes. The reason for my change is you may want to exclude a C or a C++ file from using precompiled headers for a particular case. Bo assumes C++ as it's a direct answer to the question. Zuuum assumes C, and that's the case for me and in my illustration. It could be either in practice.

It's understandable but annoying that the location of options in menus changes from one release of visual studio to another, but here is the location of the menu item in my current visual studio (2019).

Ivan
  • 4,383
  • 36
  • 27
  • 1
    This should be the accepted answer, since it clearly explains how to solve the problem. –  Jul 05 '21 at 13:04
1

Try creating a separate C++ precompiled header file (say MuffinFactoryCpp.h which is a copy of the other one). Look at the project settings under "Precompiled Headers" and use this new header file as the precompiled header for the C++ source file.

zdan
  • 28,667
  • 7
  • 60
  • 71
0

I just looked up the error here and found this thread. However, upon trial and error, I found that the issue was that I did not have all files saved recently. It seems that Visual Studio is a little finicky about save dates of files that are attached together.

I went around hitting Ctrl+S on all the source files and that fixed the issue.