Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile errors with #include <string> in Cocoa App

Tags:

xcode

stl

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?

like image 244
Roger Gilbrat Avatar asked Sep 25 '11 00:09

Roger Gilbrat


People also ask

What is compilation error with example?

Compiler errors are due to inaccuracies in code, where the compiler throws an error to alert you to something which will not compile, and therefore cannot be run. An example of a compiler error would be: int = "this is not an int"; Hope that helps.

What are the types of compilation errors?

There are 5 different types of errors in C programming language: Syntax error, Run Time error, Logical error, Semantic error, and Linker error. Syntax errors, linker errors, and semantic errors can be identified by the compiler during compilation.

What causes compilation error in C++?

A compile error happens when the compiler reports something wrong with your program, and does not produce a machine-language translation. You will get compile errors.


1 Answers

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
like image 166
Diplomat Avatar answered Sep 27 '22 20:09

Diplomat