Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 273
seanxiaoxiao Avatar asked Feb 12 '12 16:02

seanxiaoxiao


People also ask

Is Objective-C compatible with C?

Objective-C is an object-oriented programming language that is a superset of C, as the name of the language might reveal. This means that any valid C program will compile with an Objective-C compiler. It derives all its non-object oriented syntax from C and its object oriented syntax from SmallTalk.

Which is faster C or Objective-C?

Objective-C is slightly slower than straight C function calls because of the lookups involved in its dynamic nature.

What version of C does Xcode use?

Xcode 4.6. 2 uses the Clang C++ compiler frontend with LLVM as backend which is conform to the C++11 standart and uses libc++ as the standart library.

Is Objective-C for Apple?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.


1 Answers

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

like image 180
John Smith Avatar answered Nov 14 '22 06:11

John Smith