views:

96

answers:

3

I am planning on learning Objective-C to write an OS X application but it will depend on a library written in C++. Can C++ be used in an Objective-C app? Bear with me, 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?

+2  A: 

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.

olliej
+2  A: 

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

MrMage
If my answer is sufficient, please mark it as solution.
MrMage