kush
kush

Reputation: 16928

Using a C++ library in an Objective-C app?

I am planning on learning Objective-C to write an OS X application but it will depend on a library written in C++. Can I interface with C++ in an Objective-C app? I'm new to desktop development.

The C++ library will be used simply to analyze a file and return some data about that file. For example, in the libraries compiled example, in terminal you'd type

./xlsanalysis my_spreadsheet.xls

and it returns:

rows: 34
columns: 10
first row: "My Spreadsheet header"

Can I include this library directly into the Objective-C app or interface with it some how?

Upvotes: 2

Views: 359

Answers (3)

MrMage
MrMage

Reputation: 7487

For this purpose, there is Objective-C++, e.g. Objective-C plus C++ (or vice versa). From Objective-C++ files (e.g. .mm files), you have full access to all C++ functionality. Be careful when casting types from C++ to Objective-C, e.g. you should convert a C++ string to a NSString by using something like [NSString stringWithCString:cPlusPlusString.c_str()] The other direction would be string cPlusPlusString([objectiveCString cString]) (or cStringUsingEncoding:).

Upvotes: 3

olliej
olliej

Reputation: 36783

Yes, you'll just need to switch any Obj-C files that include (directly or indirectly) C++ content into objective-c++. Basically that just means changing the extension to .mm -- this will give you the ability to use C++ and Obj-C together in those files.

Upvotes: 2

Related Questions