xcode 在 Cocoa App 中使用 #include <string> 编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7542850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:01:13  来源:igfitidea点击:

Compile errors with #include <string> in Cocoa App

xcodestl

提问by Roger Gilbrat

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

我正在尝试在 xcode 4.0 中编译 Cocoa 应用程序,但出现此错误...

fatal error: 'string' file not found

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

...当试图在这一行编译为 .pch 文件时:

#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.

我有另一个 xcode 项目做同样的事情,但没有得到错误。我已经搜索了一些不同的构建设置,但我找不到一个。唯一不同的是,编译OK的项目是作为命令行项目启动的,不是Cocoa项目,但是构建设置是一样的。

The target OS is Mac OS X 10.6

目标操作系统是 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.

编译预编译头文件时会发生错误,并且不会访问任何其他文件。编译版本唯一的框架是Foundation.framework,非编译版本也有。

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

为什么在一个项目中找不到,而在另一个项目中找不到?有什么建议吗?

回答by Diplomat

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

你的源文件的扩展名是什么?如果是“.m”,请尝试将其更改为obj-cpp“.mm”,以便Xcode 推断出正确的语言。或者只是将特定于 c++ 的头文件放在“#ifdef __cplusplus”块中

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:

项目中编译的每种语言都必须存在保护,因为这个特定的包含在 pch 中。IOW,如果都是 c++ 和/或 objc++,就不会有错误。显然,至少有一个文件不能识别 C++(例如 C 或 ObjC 源代码也在目标中编译)。因此,您只需像这样保护它:

// 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

回答by Foo Bah

stringis a C++ header (for std::string). If you are looking for stuff like strcpyyou need to include string.h

string是一个 C++ 头文件(对于 std::string)。如果你正在寻找像strcpy你需要包括的东西string.h