由于Xcode不尊重文件类型而导致的Objective-C ++编译错误 [英] Objective-C++ compilation errors due to Xcode not respecting file type

查看:292
本文介绍了由于Xcode不尊重文件类型而导致的Objective-C ++编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看书中的教程( iPhone 3D编程),它使用:

I'm following a tutorial in a book (iPhone 3D Programming), which uses:

  • Objective-C标头和源文件(分别为文件扩展名.h,.m)
  • Objective-C ++头文件和源文件(文件扩展名分别为.h,.mm)
  • C ++头文件和源文件(文件扩展名分别为.hpp,.mpp)

包含一个成功编译的示例Xcode项目.

在找到示例项目之前,我已经从书中手动键入了代码,但是对于下面详细介绍的文件,我遇到了以下编译错误:

Before I found the sample project, I had manually typed out the code from the book but I was getting the following compilation errors for the files detailed below:

  1. 未知类型名称虚拟"
  2. 期望的会员名称或';'声明后的声明符

IRenderingEngine.hpp(Xcode文件检查器-文件类型=默认-C ++标头")

IRenderingEngine.hpp (Xcode File Inspector - File Type = "Default - C++ Header")

...
struct IRenderingEngine {
    virtual void Initialize(int width, int height) = 0; //2 errors as marked above
    virtual void Render() const = 0; //2 errors as marked above
    virtual void UpdateAnimation(float timeStep) = 0; //2 errors as marked above
    virtual void OnRotate(DeviceOrientation newOrientation) = 0; //2 errors as marked above
    virtual ~IRenderingEngine() {} //2 errors as marked above
};
...

  1. 必须使用"struct"标记来引用类型"IRenderingEngine"

GLView.h(Xcode文件检查器-文件类型=默认-C标头")

GLView.h (Xcode File Inspector - File Type = "Default - C Header")

#import "IRenderingEngine.hpp"
#import <QuartzCore/QuartzCore.h>

@interface GLView : UIView {
@private
    EAGLContext* m_context;
    IRenderingEngine* m_renderingEngine; //1 error marked above
    float m_timestamp;
}

- (void) drawView:(CADisplayLink*)displayLink;
- (void) didRotate:(NSNotification*)notification;

@end

所有其他文件的文件类型在Xcode File Inspector中也默认为其预期的文件类型,因此应该在构建设置"下正确运行-Apple LLVM编译器4.2-语言-将源编译为=根据文件类型"-与成功编译的示例项目中的构建设置"相同.

The file types for all the other files also defaulted to their expected file types in the Xcode File Inspector and as such should have worked correctly with the Build Setting - Apple LLVM compiler 4.2 - Language - "Compile Sources As = According to File Type" - which is identical to the Build Setting in the sample project that compiles successfully.

出于某种奇怪的原因,在我手动创建的项目中将构建设置"更改为将源编译为Objective-C ++"消除了编译错误,应用程序按预期运行.

For some odd reason changing the Build Setting to "Compile Sources As = Objective-C++" in my manually created project removed the compilation errors and the application ran as expected.

任何人都可以提供一个原因,为什么在看似相同的(源代码方式)项目之间此设置不一致?

Can anyone offer a reason as to why this setting is not consistent between seemingly identical (source-code-wise) projects?

推荐答案

头文件未编译.头文件由预处理器使用-在具有#include#import的任何位置,原始文档的实际文本都将被视为已将其复制并粘贴到原始文档中.

Header files are not compiled. Header files are used by the preprocessor — anywhere you have a #include or a #import the actual text of the original is treated as though you'd copied and pasted it into the original.

因此,无论您的文件名为.hpp,.h还是其他名称都没有关系.如果.m文件导入了包括.hpp文件的.h文件,则.hpp代码将作为.m文件的一部分进行编译,即作为Objective-C.

Hence it doesn't matter if your file is called .hpp, .h or anything else. If a .m file imports a .h file that includes a .hpp file then the .hpp code will be compiled as part of the .m file, i.e. as Objective-C.

因此,我猜测您已经拥有GLView.m.如果要直接或间接导入.hpp文件,则需要将其编译为Objective-C ++.一种方法是重命名.mm,另一种方法是告诉项目不要尝试通过文件扩展名猜测语言类型.

I am therefore going to guess that you've got GLView.m. If that's going to import a .hpp file, whether directly or indirectly, it needs to be compiled as Objective-C++. One way to do it is to rename it .mm, the other is to tell the project not to try to guess language types by file extension.

这篇关于由于Xcode不尊重文件类型而导致的Objective-C ++编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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