简体   繁体   English

在Objective-C应用中使用C ++库?

[英]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++. 我计划学习Objective-C以编写OS X应用程序,但这将取决于以C ++编写的库。 Can I interface with C++ in an Objective-C app? 我可以在Objective-C应用程序中与C ++交互吗? I'm new to desktop development. 我是桌面开发的新手。

The C++ library will be used simply to analyze a file and return some data about that file. C ++库将仅用于分析文件并返回有关该文件的一些数据。 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? 我可以将此库直接包含在Objective-C应用程序中还是可以与它进行一些交互?

For this purpose, there is Objective-C++, eg Objective-C plus C++ (or vice versa). 为此,有Objective-C ++,例如Objective-C加C ++(反之亦然)。 From Objective-C++ files (eg .mm files), you have full access to all C++ functionality. 从Objective-C ++文件(例如.mm文件),您可以完全访问所有C ++功能。 Be careful when casting types from C++ to Objective-C, eg 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: ). 将类型从C ++转换为Objective-C时要小心,例如,应使用[NSString stringWithCString:cPlusPlusString.c_str()]之类的方法将C ++ string转换为NSString ,另一个方向是string cPlusPlusString([objectiveCString cString]) (或cStringUsingEncoding:

Yes, you'll just need to switch any Obj-C files that include (directly or indirectly) C++ content into objective-c++. 是的,您只需要将包含(直接或间接)C ++内容的所有Obj-C文件切换到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. 基本上,这意味着将扩展名更改为.mm ,这将使您能够在这些文件中一起使用C ++和Obj-C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM