Compile errors with #include <string> in Cocoa App

15,070

What is the extension of your source files? If it is ".m", try to change it to obj-cpp ".mm", so that Xcode will deduce correct language. Or just put c++-specific headers inside "#ifdef __cplusplus" block

Update

The guard must exist for each language compiled in the project because this specific include is in the pch. IOW, if it were all c++ and/or objc++ there would be no error. Evidently, there is at least one file that does not recognize C++ (e.g. C or ObjC sources are also compiled in the target). Therefore, you simply guard it like so:

// MONPrefix.pch

#ifdef __cplusplus
#include <string>
#endif

// same for objc, so your C and C++ sources compile with no error:
#ifdef __OBJC__
#include <Foundation/Foundation.h>
#endif
Share:
15,070

Related videos on Youtube

Roger Gilbrat
Author by

Roger Gilbrat

Updated on June 13, 2022

Comments

  • Roger Gilbrat
    Roger Gilbrat over 1 year

    I am trying to compile a Cocoa app in xcode 4.0 and I'm getting this error...

    fatal error: 'string' file not found
    

    ...when trying to compile to .pch file on this line:

    #include <string>
    

    I have another xcode project that does the same thing, but does not get the error. I have scoured the build settings for some different, but I can't find one. The only difference is that the project that compiles OK was started as a command line project, not a Cocoa project, but the build setting are the same.

    The target OS is Mac OS X 10.6

    The error happens when compiling the precompiled header and doesn't get to any of the other files. The only framework that the compiling version has is Foundation.framework and the non-compiling one has it as well.

    Why is it not finding in one project and not the other? Any advice?

    • Roger Gilbrat
      Roger Gilbrat
      You can use the stl in objective-c++. Just name your files .mm. I completely started over with this project from a fresh cocoa app template and it works fine now.
  • justin
    justin over 11 years
    +1 "just put c++-specific headers inside "#ifdef __cplusplus" block" -- is the right solution -- but the guard must exist for each language compiled in the project because this specific include is in the pch. iow, if it were all c++ and/or objc++ there would be no error. evidently, there is at least one file that does not recognize c++ (e.g. c or objc). will update myself -- i now see this is months old :)
  • Nathanael Weiss
    Nathanael Weiss over 11 years
    This helped me figure out how to properly set up a Prefix.pch for a Cocos2D-X project. Wrapping the C++ specific stuff in "#ifdef __cplusplus" works like a charm.