dMix dMix - 2 months ago 10x
C++ Question

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?


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:).