Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

Using a precompiled header simulataneously for c++ and c files

This topic is 2636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I have an application under Windows that I compile with VS2005. It contains mainly C++ files, but also some C files. Until now, I had only my C++ files use the precompiled header (stdafx.h), but I also would like C files to have access to the precompiler file defines now. How do I do it?

When I mark a C file as "using a precompiled header", and add a "#include "stdafx.h"" at the top, the compiler gives me following error:

fatal error C1853: '.\Debug/theFile.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)

Thanks for any insight!

Share this post


Link to post
Share on other sites
Advertisement
The error is pretty self-explanatory.

C and C++ compile very, very differently. There is no way to have a C++-generated PCH also work for the C compilation model. What happens if your PCH defines a class? Or a struct with a member function? Or any number of other things valid in C++ but not C?

You might be able to generate the PCH for the C files first and then use it from C++, but that strikes me as very risky. The "or vice versa" part of the error suggests that it can't be done anyways.


C and C++ are deceptively close to compatible, but not actually fully compatible. Your best bet is to have a separate pchcpp.cpp which generates pchcpp.pch, and a pchc.c which generates pchc.pch. In your C++ code, include pchcpp.h, and in your C code, include pchc.h.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!