How to use c++ and objective-c together in XCode 4.2
Asked Answered
M

3

8

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.

in the objective-c head file. 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:

if (self) {
    adapter = Adapter::getInstance();
    // Custom initialization
}

Is it write?

Monochromatism answered 12/2, 2012 at 16:58 Comment(4)
It seems you are indeed using .mm file, so, what's the trouble? you have not told us what problem you are facing.Toscana
It says that there are some compilation error, so I was wondering if there's anything wrong in the syntax.Monochromatism
Have you fixed it? If not, just precise what "some compilation error" is.Toscana
Thanks, it is not fixed yet. But the reason is that a library file in the project do not have i386 version which is needed in the simulator compilation. I was searching for one.Monochromatism
A
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.

This compiler can understand both C++ and Objective-C.

In other words, the ObjC++ compiler lets you put C++ code directly in Objective-C methods, and vice versa.

Take a look at Cocoa_With_Carbon_or_CPP example and Strategies for Using C++ in Objective-C Projects (and vice versa) article .

Adopt answered 12/2, 2012 at 19:0 Comment(0)
L
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.

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++".

Lieu answered 12/2, 2012 at 17:9 Comment(3)
Size doesn't matter What you have to do is just change change the extension to .mm of to class that containing C++ code ...Headmistress
@HikmatKhan What I was suggesting is that sometimes renaming files, especially in big (multiperson) projects is not really an option.Lieu
@kevlar 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 clang I think it is "-x objective-c++"Lieu
T
6

Just rename your file to have an extension .mm instead of .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.

Toscana answered 12/2, 2012 at 17:5 Comment(0)
A
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.

This compiler can understand both C++ and Objective-C.

In other words, the ObjC++ compiler lets you put C++ code directly in Objective-C methods, and vice versa.

Take a look at Cocoa_With_Carbon_or_CPP example and Strategies for Using C++ in Objective-C Projects (and vice versa) article .

Adopt answered 12/2, 2012 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.