c++xcodepch

iostream file not found error when compiling a precompile header in Xcode


I get a 'iostream' file not found error when compiling my Prefix.pch precompile header file in Xcode 9. It seems the compiler can't find it, yet it finds <stdio.h> and <Foundation/Foundation.h> files just fine.

#include <stdio.h> // ok
#include <iostream> // error
#import <Foundation/Foundation.h> // ok

[EDIT] The project is a mix of C++ and Objective-C files, and I am trying to pull into the .pch file some header files that are C++. I am unable to figure out how to mix the two in the .pch file.


Solution

  • You should modify the pch with #ifdef preprocessor directives:

    #ifdef __cplusplus
    #include <cstdio>
    #include <iostream>
    #endif
    
    #ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #enid
    

    (I just ran into this problem and solved it in this way.)