I had a trouble to combine c++ and objective-c together in developing a iphone app. I had a 3rd party library to use in the app. I had a plan to use c or c++ to wrap the library and then use objective-c to call it. After I had finished the class with c++, I had a trouble to use it in objective-c. Is there any sample code? Thanks.
在开发iPhone应用程序时,我很难将c ++和objective-c结合起来。我在应用程序中使用了第三方库。我有一个计划使用c或c ++来包装库,然后使用objective-c来调用它。用c ++完成课程后,我在objective-c中使用它时遇到了麻烦。有没有示例代码?谢谢。
in the objective-c head file. I write
在objective-c头文件中。我写
#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?
是写吗?
3 个解决方案
#1
5
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 ++的策略(反之亦然)文章。
#2
8
In XCode there is a flag to compile all files as Objective-C++. I've used it to compile huge C++ libraries into iOS programs.
在XCode中,有一个标志可以将所有文件编译为Objective-C ++。我用它来将巨大的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++. In the clang/gcc commandline I think it is "-x objective-c++".
如果你看一下“构建设置”,就会写一个“编译源代码”的地方。在那里有一个下拉菜单,您可以在其中选择Objective-C ++。在clang / gcc命令行中我认为它是“-x objective-c ++”。
#3
6
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. XCode by default compiles .m
files with Objective-C compiler and .mm
ones with Objective-C++ one.
要将C ++代码与Objective-C代码混合,您将需要Objective-C ++编译器。默认情况下,XCode使用Objective-C编译器编译.m文件,使用Objective-C ++编译.mm文件。
#1
5
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 ++的策略(反之亦然)文章。
#2
8
In XCode there is a flag to compile all files as Objective-C++. I've used it to compile huge C++ libraries into iOS programs.
在XCode中,有一个标志可以将所有文件编译为Objective-C ++。我用它来将巨大的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++. In the clang/gcc commandline I think it is "-x objective-c++".
如果你看一下“构建设置”,就会写一个“编译源代码”的地方。在那里有一个下拉菜单,您可以在其中选择Objective-C ++。在clang / gcc命令行中我认为它是“-x objective-c ++”。
#3
6
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. XCode by default compiles .m
files with Objective-C compiler and .mm
ones with Objective-C++ one.
要将C ++代码与Objective-C代码混合,您将需要Objective-C ++编译器。默认情况下,XCode使用Objective-C编译器编译.m文件,使用Objective-C ++编译.mm文件。