James Linden
James Linden

Reputation: 1125

Precompiled Headers with Mixed C and C++

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?

Upvotes: 26

Views: 34329

Answers (5)

Ivan
Ivan

Reputation: 4753

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).

Upvotes: 3

Brandon Nolet
Brandon Nolet

Reputation: 128

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.

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92211

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

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

Upvotes: 19

zdan
zdan

Reputation: 29450

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.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308111

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.

Upvotes: 6

Related Questions