Include <string> not found compile error in Xcode 4.2

33,945

Solution 1

select project -> build setting -> apple LLVM compiler 5.1 -> language

In Compile Sources As change to Objective-C++

Solution 2

There's a quirk in XCode. I noticed it in 7.3. Most projects recognize .mm files and the STL, while one project I had did not. The fix was that I had to click on the top-left project icon, then click Targets > Build Phases > Link Binary with Libraries > and add in AppKit.framework. Next, I had to click Targets > Build Settings > search on "Compile Sources", and set it to "Objective C++" on all possible columns. Then, do a Clean and then a Build from the Product menu. This compiled properly then. Then, go back to that Compile Sources again and set it back to "According to File Type" on all possible columns. Then, click Build from the Product menu again. At that point, it compiled properly and allowed me to utilize the "according to file type" option, which I like better.

Oh, and if doing Cocoa stuff, don't forget to add the following header in your files:

#import <Cocoa/Cocoa.h>

And if doing command line stuff, don't forget to add the following instead of the Cocoa header:

#import <Foundation/Foundation.h>

Solution 3

i believe you need to include the whole path to the library. similarly to say "foundation" & "uiview" frameworks.

#import <Foundation/Foundation.h>

or

#import <UIKit/UIKit.h>

and yes, make sure you add the library to your target.

Solution 4

So I was having this issue with the Cocoapods library Bypass and none of these solutions did anything. The problem was with a file which Cocoapods creates called an umbrella header. This is located in <POD_NAME>/Support Files/<POD_NAME>-umbrella.h. Delete it, and it should build just fine.

Now for the explanation of why this is necessary: the umbrella header is mixing both C++ and Objective-C code directly in a header which is a big no-no apparently and ends up completely breaking the C++ imports. By removing it (which seems to have no effect?) this conflicting import which Cocoapods unknowingly created will go away.

Share:
33,945
ilker Acar
Author by

ilker Acar

Updated on August 18, 2021

Comments

  • ilker Acar
    ilker Acar almost 3 years

    I'm getting include not found compile error in XCode. I have an iOS app project that i use Objective-c and c++ as mix.

    Initially, i created one .h file and one .cpp file in my ios project. Then, I renamed the .cpp file to .mm file.

    Here is my .h file;

    TestLog.h

    #ifndef CalculatorDemo_TestLog_h
    #define CalculatorDemo_TestLog_h
    #include <string>
    using namespace std;
    
    class TestLog
    {
    private:
        string logString;
    public:
        void Log(string logMessage);
    };
    
    
    #endif
    

    TestLog.mm

    #include "TestLog.h"
    
    void TestLog::Log(string logMessage)
    {
        //this->logString->append(logMessage);
    
    }
    

    What am I missing? Do I need to add std c++ library to my targetS? Something related to Search Header Paths?

    I just need to use string type.

    Thanks much for in advance

  • Admin
    Admin over 7 years
    Great Answer! Thank you! I thought I was losing my mind... Upgraded my xcode from 7 to 8 and all of a sudden my C++ code was NOT being recognized... Thank you!
  • bauerMusic
    bauerMusic over 3 years
    Small change: select target -> build setting. Search 'compile sources', change to Objective-C++. (It's now under Apple Clang - Language).