[英]How to use c++ and objective-c together in XCode 4.2
I had a trouble to combine c++ and objective-c together in developing a iphone app. 在开发iphone应用程序时,我很难将c ++和Objective-c结合在一起。 I had a 3rd party library to use in the app.
我在应用程序中使用了第3方库。 I had a plan to use c or c++ to wrap the library and then use objective-c to call it.
我有一个计划,要使用c或c ++来包装库,然后使用Objective-c来调用它。 After I had finished the class with c++, I had a trouble to use it in objective-c.
在用c ++完成课程之后,我很难在Objective-c中使用它。 Is there any sample code?
有样本代码吗? Thanks.
谢谢。
in the objective-c head file. 在Objective-C头文件中。 I write
我写
#import <UIKit/UIKit.h>
#import "ZJTConstants.h"
#include "TTSAdapter.h"
class Adapter;
@interface ZJTVBlogViewController : UIViewController {
@private
Adapter* adapter;
}
@end
and in the mm file, I write: 在mm文件中,我写道:
if (self) {
adapter = Adapter::getInstance();
// Custom initialization
}
Is it write? 是写吗?
In XCode there is a flag to compile all files as Objective-C++. 在XCode中,有一个标志可以将所有文件编译为Objective-C ++。 I've used it to compile huge C++ libraries into iOS programs.
我用它来将庞大的C ++库编译成iOS程序。
If you look at the "Build Settings" there is a place written "Compile Sources As". 如果您查看“构建设置”,则有一个写为“将源编译为”的位置。 There is a dropdown menu there where you can select Objective-C++.
有一个下拉菜单,您可以在其中选择Objective-C ++。 In the clang/gcc commandline I think it is "-x objective-c++".
在clang / gcc命令行中,我认为它是“ -x Objective-c ++”。
Just rename your file to have an extension .mm
instead of .m
. 只需将文件重命名为扩展名
.mm
而不是.m
。
To mix C++ code with Objective-C code, you will need Objective-C++ compiler. 要将C ++代码与Objective-C代码混合使用,您将需要Objective-C ++编译器。 XCode by default compiles
.m
files with Objective-C compiler and .mm
ones with Objective-C++ one. 默认情况下,XCode使用Objective-C编译器编译
.m
文件,使用Objective-C ++一个编译.mm
文件。
Calling C++ code from Objective-C code involves ending your file with .mm (instead of .m) so that the Objective-C++ compiler will be used.
从Objective-C代码调用C ++代码涉及以.mm(而不是.m)结束文件,以便使用Objective-C ++编译器。
This compiler can understand both C++ and Objective-C.
该编译器可以理解C ++和Objective-C。
In other words, the ObjC++ compiler lets you put C++ code directly in Objective-C methods, and vice versa.
换句话说,ObjC ++编译器使您可以将C ++代码直接放入Objective-C方法中,反之亦然。
Take a look at Cocoa_With_Carbon_or_CPP example and Strategies for Using C++ in Objective-C Projects (and vice versa) article . 看一下Cocoa_With_Carbon_or_CPP示例和在Objective-C项目中使用C ++的策略(反之亦然) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.