2

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?

Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • 2
    If your real header guard is like that, you might want to look at this: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – chris Sep 27 '12 at 22:16
  • 3
    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 Sep 27 '12 at 22:19
  • +1 for `to rub your nose into it` – Vinícius Sep 27 '12 at 22:21
  • " I must NOT edit the project precompiled header settings" i wanna drive a car, but i must not drive it, how can i do that – Cheers and hth. - Alf Sep 27 '12 at 22:27
  • You're right Alf, it doesn't seems possible at all – Vinícius Sep 27 '12 at 22:34
  • chris tip was great althought, thanks! – Vinícius Sep 27 '12 at 22:44
  • @Alf, easy you crawl into the trunk and let someone else drive (i.e. create a library and link it to the main app) – Josh Petitt Sep 28 '12 at 00:03
  • You say you can't edit the _project_ precompiled header settings. But what about the per-file settings? It sounds like all you need to do it turn off usage of the precompiled header for that specific .cpp file. Or is even that not allowed in your scenario for some reason? – TheUndeadFish Sep 28 '12 at 03:07
  • @TheUndeadFish, yes turn off the precompiled header for a specific file is exactly what I`m looking for – Vinícius Sep 28 '12 at 17:46

1 Answers1

2

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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324