15

Update:

What are the effects of including stdafx.h in my header files?


I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows.

In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h.

Here's my stdafx.h

#pragma once

#include <string>
#include <vector>
#include <map>
...

and my stdafx.cpp

#include "stdafx.h"

In every .h and .cpp file, I have the following:

#pragma once //if in a header file
#include "stdafx.h"

For both release and debug, I have "Create Precompiled Header (/Yc)". It compiled fine in debug mode, but in release mode it keeps reporting

error LNK2005: ___@@_PchSym_@00@UfhvihUaszlaDUwlxfnvmghUnnlUhixUnnlPeDUnnlPeDUivovzhvUvmgrgbOlyq@ already defined in A.obj

If I switch both to "Use precompiled header", I get in both Debug and Release

fatal error C1854: cannot overwrite information formed during creation of the precompiled header in object file:

Does anyone know what's going on?

jameszhao00
  • 7,213
  • 15
  • 62
  • 112
  • By the way, I created a new test project and compared its PCH settings to mine. Its settings were Use PCH. That project compiled fine somehow. – jameszhao00 Sep 09 '09 at 01:50

4 Answers4

30

You put "create precompiled header" only for stdafx.cpp. Then "use precompiled header" for all of the other ".cpp" files. Finally, have include "stdafx.h" at the start of each ".cpp" file (not usually in the header files.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • I can only set a config-wide Create PCH or Use PCH. – jameszhao00 Sep 09 '09 at 02:02
  • 10
    In VS, you can right click on an individual file and choose "properties". Then under "C++ -> Precompiled headers" you can set the value to "create" or "use". The config wide setting is used as a default - you should probably set this to "use", then override it for the stdafx.cpp – 1800 INFORMATION Sep 09 '09 at 02:09
  • So what are the effects of including stdafx.h in my header files? – jameszhao00 Sep 09 '09 at 02:10
  • There's not really any effect because a header itself does not define a compilable unit, they're only part of other compilable units. – Aidan Ryan Sep 09 '09 at 02:15
  • I know this is an old question, but I'm confused at the "not usually in the header files" part. Are you saying to include all necessary files regularly for example.h and example.cpp should include the precompiled header and example.h? Wouldn't that mean the files included in example.h are compiled twice - once in the pch and once in example.h? – Proxy Jan 20 '14 at 15:16
  • You wouldn't generally include stdafx.h in a header file. Most headers have some kind of include protection such as "pragma once" specified which stops them being included twice from breaking the compilation. Therefore since it is already included once in the precompiled header, including it again in some other file example.h would have no effect. – 1800 INFORMATION Jan 21 '14 at 03:35
  • For those who failed to find `Precompiled Headers` option for stdafx.cpp or any other .cpp file, double-check that `Item type` field for this file is `C/C++ compiler` – dmytro.poliarush Dec 23 '19 at 15:25
6

The /Yc compiler option is used to create a pre-compiled header for a compilation action. The /Yu option instructs the compiler to use a pre-compiled header.

You will always use the /Yu option in project settings. In the property pages for your stdafx.cpp file, the /Yc option will be set.

It is important to understand that there are separate compilation options for each .cpp file .

See here for details of the /Y options.

Aidan Ryan
  • 11,389
  • 13
  • 54
  • 86
4

You put the #pragma once before the #include "stdafx.h" which I think is causing the compiler to ignore the #pragma once directive.

Also, I don't think you should be putting the #include "stdafx.h" line into the header files at all.

GBegen
  • 6,107
  • 3
  • 31
  • 52
  • 2
    Yup, you usually put #include "stdafx.h" as the first include of every .cpp file. I think it might actually have to be the first non-comment line. – Soo Wei Tan Sep 09 '09 at 01:57
  • 1
    So basically I have to include my own header files in the PCH? What about the expensive to compile headers (boost) referenced in my own headers? – jameszhao00 Sep 09 '09 at 02:00
  • 2
    For example, if I reference Boost in both stdafx and my_class.h, in my_class.cpp the Boost referenced in my_class.h will be skipped because stdafx already referenced it? – jameszhao00 Sep 09 '09 at 02:04
  • 2
    Even if a header must be included elsewhere, a good bit of the processing only has to happen as pare of the pre-compile. You are including headers in the PCH to minimize processing, you are including headers elsewhere to provide type information and define constructs. The includes are serving separate purposes. – Aidan Ryan Sep 09 '09 at 02:10
  • 2
    While this answer points out some issues, it does not actually answer the question as far as the linker errors. – Aidan Ryan Sep 09 '09 at 02:16
  • @Aidan: No, it doesn't work that way. Any header you include in stdafx.h is by transitive inclusion available everywere. Hence, you don't need to include those headers again. It's harmless, though, because Boost headers have include guards. – MSalters Sep 09 '09 at 14:31
1

The results of using "stdafx.h" are not influenced by the PreCompiled Header system. If you turn off Create PCH/Use PCH, the code compiles and creates the same output, except it does so slower. This is also why you can use it in portable code (unlike #pragma once)

MSalters
  • 173,980
  • 10
  • 155
  • 350