Xcode:从Objective-C项目引用C ++项目 [英] Xcode: Referencing C++ project from Objective-C project

查看:114
本文介绍了Xcode:从Objective-C项目引用C ++项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始把头发撕掉了,所以是时候发布了!

I'm beginning to tear my hair out over this, so it's time to post here!

我在XCode中有一个C ++项目,作为命令行构建工具。所有.cpp文件都已重命名为.mm,有一个.hh和几个.h标头。我在Build Settings中指定了'Compile Sources As - > Objective-C ++'。

I have a C++ project in XCode, built as a Command Line Tool. All .cpp files have been renamed as .mm, there is one .hh and a few .h headers. I've specified 'Compile Sources As -> Objective-C++' in Build Settings.

我有一个iOS应用程序的另一个XCode项目。这被设置为'根据文件类型编译源 - >。我无法将其设置为以Objective-C ++编译所有内容,因为项目的某些部分不会编译为Obj-C ++。

I have another XCode project for an iOS app. This is set to 'Compile Sources As -> According to File Type'. I can't set it to compile everything as Objective-C++ since some parts of the project won't compile as Obj-C++.

iOS中有一个类应用程序,我希望利用我的C ++项目,它包含文件MyClass.hh,MyClass.mm;它们只是.h和.m但我重命名它们是为了解决这个问题。

There is one class in the iOS app where I wish to utilise my C++ project, and it consists of files MyClass.hh, MyClass.mm; they were simply .h and .m but I renamed them in the hope of remedying this problem.

我把我的cpp项目拖到了iOS项目中。然后我在iOS Build阶段添加了一个Target Dependency来指向cpp CLI应用程序。

I dragged my cpp project into the iOS project. I then added a Target Dependency in the iOS Build Phases to point to the cpp CLI application.

在MyClass.hh中我有

In MyClass.hh I have

#include "../path/to/CppProject/ImportAll.h"

..这是一个头文件,它连续地包含来自cpp项目的所有头文件。然后我开始我的业务,​​在MyClass.hh中创建一个属性,它是一个指向cpp对象的指针,以及MyClass.mm到cpp类的各种引用。

..which is a header file that successively 'include's all headers from the cpp project. I then go about my business, creating one property within MyClass.hh which is a pointer to a cpp object, and various references within MyClass.mm to cpp classes.

darn的事情只是不会编译 - 我得到了大量与我的cpp类相关的消息,如下所示:

The darn thing just won't compile though - I get a ton of messages relating to my cpp classes like this:

MyCPPFile.h:10: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token

编译器是否认识到MyClass.hh不是Objective-C ++而不是Objective-C?我是否应该有一个目标依赖项,而不是指向这个CLI应用程序,或许?

Is the compiler not recognising that MyClass.hh is Objective-C++ not Objective-C? Should I have a target dependency which is a library instead of pointing to this CLI app, perhaps?

我已经搜索了一段时间试图解决问题。我见过人们使用

I've googled around for a while trying to solve the problem. I've seen references to people using

#ifdef __cplusplus

或者必须将文件重命名为.mm以使工作正常,但在其他情况下,人们只是说他们拖放项目没有任何问题。

or else having to rename files to .mm to get things working, but in other cases people simply say they drag and drop projects across with no issues.

有人可以请教导我,并解释一下我应该采取哪些关键步骤来完成这项工作?我将非常感激!谢谢。

Can someone please enlighten me, and explain what key steps I should be performing to make this work? I'd be very grateful! Thank you.

推荐答案

感谢所有给我建议的人。

Thanks to everyone who gave me suggestions.

从头开始重新创建我的项目,我摆弄并逻辑化了解决方案。请注意 - 至少在我的情况下 - 不需要使用#ifdef __cplusplus,也不需要使用任何其他预处理器指令。只有文件后缀更改是必不可少的(详见下文)。

Having created my projects anew from scratch, I fiddled and logic-ed my way to the solution. Note that - at least in my case - no use of #ifdef __cplusplus was required, nor any other preprocessor directive. Only file suffix changing is essential (as detailed below).

以下是流程:

创建C ++静态库和包含/链接与XCode 4中的Objective-C库(例如iOS应用程序)


  • 在XCode 4中创建一个Cocoa Touch静态库类型的新项目。此后我们将此项目称为CProj。

  • 在CProj工作区中根据需要创建/导入尽可能多的C ++类。不需要文件名的特殊后缀 - 我的所有文件都是.h和.cpp。

  • 在XCode中创建/打开任何Objective-C项目,例如: iOS应用。此后我们将这个项目称为'ObjProj'。

  • 在Finder中查找CProj.xcodeproj(通常是/Documents/CProj/CProj.xcodeproj),将其拖到可见的ObjProj项目图标上XCode项目导航器的顶部(XCode左侧的可见文件/文件夹层次结构)。现在将CProj作为ObjProj中的嵌套项目。

  • 在Project Navigator中左键单击ObjProj项目图标,右侧会出现一个新窗格,左键单击此窗格中的Build Phases。

  • 在Target Dependencies下,单击'+'图标并选择下面列出的CProj项目下的CProj库图标。单击确定。

  • 在Link Binary With Libraries下单击'+'图标,然后从显示的窗口顶部的Workspace可折叠文件夹中选择CProj library icon。

  • In XCode 4 create a New Project of type Cocoa Touch Static Library. Hereafter we shall refer to this project as 'CProj'.
  • In CProj workspace create/import as many C++ classes as required. No special suffix for filenames is necessary - all my files are .h and .cpp.
  • In XCode create/open any Objective-C project e.g. iOS app. Hereafter we shall refer to this project as 'ObjProj'.
  • Find CProj.xcodeproj in Finder (typically /Documents/CProj/CProj.xcodeproj), drag it onto the visible ObjProj project icon at the top of your XCode Project Navigator (visible file/folder hierarchy on left side in XCode). This now places CProj as a nested project within ObjProj.
  • Left click ObjProj project icon in Project Navigator, a new pane emerges to the right, left click Build Phases in this pane.
  • Under Target Dependencies click '+' icon and select CProj library icon below listed CProj project. Click OK.
  • Under Link Binary With Libraries click '+' icon and select CProj library icon from within Workspace collapsible folder at top of window presented.

文件重命名规则


  • 包括ObjProj中的CProj头,使用路径回溯来识别导入,例如#include../../MyProj/MyImport.h到第2步dirs,然后进入MyProj文件夹。

  • Where including a CProj header in ObjProj, use path backtracking to identify import, e.g. #include "../../MyProj/MyImport.h" to step 2 dirs backwards then step into MyProj folder.

任何ObjProj实现文件,包括CProj标头必须从.m重命名为.mm。包含CProj头的任何ObjProj头文件必须从.h重命名为.hh。

Any ObjProj implementation file including a CProj header must be renamed from .m to .mm . Any ObjProj header file including a CProj header must be renamed from .h to .hh .

包含后缀为.hh的文件的任何ObjProj文件(结果必须以相同的方式重命名上面的步骤。

Any ObjProj file including a file whose suffix is now .hh (consequence of step above) must be renamed in the same manner.

一个例子:
考虑你有文件啊,Am,Bh,Bm
A.m导入B.h,而B.h包含一个或多个CProj标头。 B.m包括类型引用,使用通过B.h包含的CProj类。
首先重命名是B.h - > B.hh,因为它包含include。
然后B.m - > B.mm,因为它包含对CProj的类型引用(由于B.h已经完成它们,所以没有必要)。
然后Am - > A.mm因为Am包含Bh的包含现在有一个包含重命名的文件,B.hh。

PS请注意,C ++库不喜欢自动重建它应该 - 在从内部更新CProj后,我经常要在ObjProj中'清理'然后'构建'。

P.S. Note that the C++ library doesn't like to auto-rebuild when it should - I often have to 'Clean' then 'Build' in ObjProj after updating CProj from within it.

这篇关于Xcode:从Objective-C项目引用C ++项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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