使用 #include <string> 编译错误在可可应用程序中 [英] Compile errors with #include &lt;string&gt; in Cocoa App

查看:68
本文介绍了使用 #include <string> 编译错误在可可应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

fatal error: 'string' file not found

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

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

#include <string>

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

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.

目标操作系统是 Mac OS X 10.6

The target OS is Mac OS X 10.6

该错误发生在编译预编译头文件时,并且没有到达任何其他文件.编译版本唯一的框架是Foundation.framework,非编译版本也有.

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?

推荐答案

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

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

更新

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

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

这篇关于使用 #include <string> 编译错误在可可应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆