2
votes

I encountered this problem, Usually you all are going to ask me the why, so I'll explain first so you can just answer me, I'm working on a source with at least 500.000lines but now one of my source (.cpp) files I just created must NOT add the precompiled header which is "StdAfx.h" in this case, I must NOT edit the project precompiled header settings, obviously if I try to compile my the project without including stdafx in the new file i'll be smashed with a C1010, what I've tried is this

StdAfx.h:

#ifndef __ABC123
//do all your includes
#endif

MyFile.cpp:

#define __ABC123
#include "StdAfx.h"
#undef __ABC123

Althought, this doesn't works, stdafx.h is still including the files. Why I can't have stdafx.h includes on this .cpp file is because some includes on stdafx have tons of conflicts with the includes of this cpp. I can't modify the stdafx includes because thousands of other files use it, how to deal with this?

1
If your real header guard is like that, you might want to look at this: stackoverflow.com/questions/228783/…chris
Precompiled headers only make sense when they are the same in every translation unit. Just don't use them when you need #ifdef, it isn't going to work. The compiler will actually skip your #define to rub your nose into it.Hans Passant
+1 for to rub your nose into itsnoopy
" I must NOT edit the project precompiled header settings" i wanna drive a car, but i must not drive it, how can i do thatCheers and hth. - Alf
You're right Alf, it doesn't seems possible at allsnoopy

1 Answers

2
votes

Impossible. You have painted yourself in to a corner here. Let me 'splain.

PCHs are intended to be used across an entire project. Every translation unit is expected to include the PCH.

This behavior can be overridden, but only via the project settings for the translation unit in question. You have already said that you must not edit the project settings, hence eliminating your only avenue of escape.

If you really need this and there's no way out, I would consider taking such drastic steps as moving the non-PCH code in to its own project.