C++ 在 Xcode 4.2 中包含 <string> not found 编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10325853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include <string> not found compile error in Xcode 4.2
提问by ilker Acar
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.
我在 XCode 中发现包含未找到编译错误。我有一个 iOS 应用程序项目,我使用 Objective-c 和 C++ 作为混合。
Initially, i created one .h file and one .cpp file in my ios project. Then, I renamed the .cpp file to .mm file.
最初,我在我的 ios 项目中创建了一个 .h 文件和一个 .cpp 文件。然后,我将 .cpp 文件重命名为 .mm 文件。
Here is my .h file;
这是我的 .h 文件;
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
测试日志.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?
我错过了什么?我需要将 std c++ 库添加到我的 targetS 吗?与搜索标题路径有关的东西?
I just need to use string type.
我只需要使用字符串类型。
Thanks much for in advance
非常感谢提前
回答by AmyG
select project -> build setting -> apple LLVM compiler 5.1 -> language
选择项目 -> 构建设置 -> 苹果 LLVM 编译器 5.1 -> 语言
In Compile Sources As
change to Objective-C++
在Compile Sources As
更改为Objective-C++
回答by Volomike
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.
XCode 中有一个怪癖。我在 7.3 中注意到了它。大多数项目都能识别 .mm 文件和 STL,而我的一个项目则不能。解决方法是我必须单击左上角的项目图标,然后单击目标 > 构建阶段 > 将二进制文件与库链接 > 并添加 AppKit.framework。接下来,我必须单击目标 > 构建设置 > 在“编译源”上搜索,然后在所有可能的列上将其设置为“Objective C++”。然后,从“产品”菜单执行“清理”和“构建”。这然后正确编译。然后,再次返回 Compile Sources 并在所有可能的列上将其设置回“根据文件类型”。然后,再次从产品菜单中单击构建。那时,它正确编译并允许我使用我更喜欢的“根据文件类型”选项。
Oh, and if doing Cocoa stuff, don't forget to add the following header in your files:
哦,如果做 Cocoa 的东西,不要忘记在你的文件中添加以下标题:
#import <Cocoa/Cocoa.h>
And if doing command line stuff, don't forget to add the following instead of the Cocoa header:
如果执行命令行操作,请不要忘记添加以下内容而不是 Cocoa 标头:
#import <Foundation/Foundation.h>
回答by Sirens
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.
所以我在 Cocoapods 库 Bypass 上遇到了这个问题,但这些解决方案都没有做任何事情。问题出在 Cocoapods 创建的一个名为伞头的文件上。这位于<POD_NAME>/Support Files/<POD_NAME>-umbrella.h
. 删除它,它应该构建得很好。
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.
现在解释为什么这是必要的:伞头将 C++ 和 Objective-C 代码直接混合在一个头中,这显然是一个很大的禁忌,最终完全破坏了 C++ 导入。通过删除它(这似乎没有效果?)Cocoapods 在不知不觉中创建的这种相互冲突的导入将消失。
回答by DoS
i believe you need to include the whole path to the library. similarly to say "foundation" & "uiview" frameworks.
我相信你需要包括图书馆的整个路径。类似于说“基础”和“uiview”框架。
#import <Foundation/Foundation.h>
or
或者
#import <UIKit/UIKit.h>
and yes, make sure you add the library to your target.
是的,请确保将库添加到目标中。