2

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.

jstm88
  • 3,335
  • 4
  • 38
  • 55
  • 1
    XCode 4 is still under NDA, no? You should visit the Apple developer forum for this. – Eiko Oct 17 '10 at 06:48

2 Answers2

1

Delete the derived data (Window-Organizer, Projects tab, select your project) to have XCode rebuild the index for PCH's. Also, if you use Unit Tests, there is a separate PCH for the UnitTests target. Make sure this also includes the required imports.

Jelle
  • 1,024
  • 10
  • 18
-1

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

Edit - In Xcode the pch file is really a prefix header that happens to be precompiled (with the default project configuration). Check the GCC_PRECOMPILE_PREFIX_HEADER and GCC_PREFIX_HEADER build settings.

Alejandro
  • 3,726
  • 1
  • 23
  • 29
  • 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 #endif`. If I remove the individual `#import ` 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? – jstm88 Oct 17 '10 at 12:38
  • 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. – Alejandro Oct 17 '10 at 19:47
  • 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. – jstm88 Oct 21 '10 at 19:40
  • 1
    You can also set the Prefix Header property in your project settings – Alejandro Oct 22 '10 at 00:19
  • This is not true for XCode. You can move imports to the pch, and remove them from the original source files. So they do have other efects and you don't need to include them in the source files if they are in the pch. – Jelle Apr 24 '13 at 05:18
  • @Jelle see my edit. The pch file in Xcode is really a prefix header. – Alejandro Apr 25 '13 at 21:50