views:

33

answers:

1

I just created a "C++ Standard Dynamic" library project using Xcode and compiled using LLVM 2.0. I notice that the PCH file contains the line #include <iostream> but the file Test.cp also includes that #include <iostream> statement.

The strange thing is that by removing the statement in Test.cp, the build fails with the error Semantic Issue – Use of undeclared identifier 'std' despite the fact that that statement is also included in the PCH.

Why doesn't the PCH work? Is there a setting I'm missing? I've never done anything with them before (since they always just work) so I'm not sure what's causing it not to work, even with a brand-new project.

+1  A: 

Precompiled headers are for speeding up compilation. They don't have any other effect, in particular you sill have to include the headers where you need them.

Aleph
Hmmm... that's not what I've seen. In particular, the PCH file generated for a brand-new Cocoa project is this: `#ifdef __OBJC__ #import <Cocoa/Cocoa.h> #endif`. If I remove the individual `#import <Cocoa/Cocoa.h>` from each header file, it still works despite the .pch file being the only place Cocoa is included. Is it possible this behavior (the PCH functioning as a kind of "global header") is unique to Cocoa projects?
jfm429
from the [Wikipedia entry](http://en.wikipedia.org/wiki/Precompiled_header): "A related feature is the prefix header, which is a file that is automatically included by the compiler without requiring the use of any compiler directives. Prefix headers are commonly precompiled, but not all precompiled headers are prefix headers." Not sure why XCode treats the precompiled header as a prefix header.
Aleph
Huh... that's strange. I used the .pch file at one point as a prefix header, and it worked, but now I can't figure out how I did it... it was a long time ago. Unless it used to be the default. Anyhow, thanks for your help. I ended up just putting the statements at the beginning of each file; it's not that big of a deal.
jfm429
You can also set the Prefix Header property in your project settings
Aleph