简体   繁体   English

链接和使用C ++库与Objective-C应用程序

[英]linking and using a C++ library with an Objective-C application

I'm writing a graphical application using Objective-C for the front end and C++ for the graphics processing and network communication. 我正在编写一个图形应用程序,使用Objective-C作为前端,使用C ++进行图形处理和网络通信。 I read around on Apple's site looking for a way to link either a .dylib or .so with my C++ code in it to my Xcode project, but nothing seemed to work. 我在Apple的网站上阅读,寻找一种方法将.dylib.so与我的C ++代码链接到我的Xcode项目,但似乎没有任何效果。 I was able to get the project to reference it and link against it, but when I tried to call functions from that .dylib, it was saying that it didn't know what I was trying to do. 我能够让项目引用它并链接它,但当我试图从.dylib调用函数时,它说它不知道我想要做什么。 Does anyone know what is going on here? 有谁知道这里发生了什么?

I know that Objective-C has all the libraries I would need to do graphics and networking, but I just feel like doing it like this. 我知道Objective-C有我需要做的图形和网络所有的库,但我只是觉得这样做。 I haven't done much C++ in a while and I want to learn more Objective-C, so what better way than to use them together? 我有一段时间没有做太多的C ++,我想学习更多的Objective-C,那么有什么比一起使用它们更好的方法呢?

Thanks, Robbie 谢谢,罗比

Most of the projects I work on have an ObjC frontend and C++ backend. 我工作的大多数项目都有一个ObjC前端和C ++后端。 If you're dealing exclusively with functions, then Dave Gamble's name mangle fix is correct, but if you're dealing with more complex situations, where you need to deal with both ObjC and C++ objects, your best bet is to wrap the C++ objects in ObjC objects. 如果你只处理函数,那么Dave Gamble的名称修复是正确的,但是如果你正在处理更复杂的情况,你需要处理ObjC和C ++对象,你最好的办法是包装C ++对象在ObjC对象中。 Using opaque references (which is a very fancy way of saying void* ), you can actually hand around C++ objects in ObjC and vice versa. 使用不透明引用(这是一种非常奇特的方式表示void* ),您实际上可以在ObjC中处理C ++对象,反之亦然。 I have some sample code that may be helpful. 我有一些可能有用的示例代码

That said, for graphics you're probably going to take a serious performance hit doing custom C++ rather than using Core Image and the related frameworks. 也就是说,对于图形,你可能会在使用自定义C ++而不是使用Core Image和相关框架时受到严重影响。 Core Image and the other graphics frameworks are highly optimized for the Mac, and you're very unlikely to do better with hand-rolled C++ (or even very well-written C++ that isn't specifically for the Mac). Core Image和其他图形框架针对Mac进行了高度优化,使用手动C ++(甚至是非常适合Mac的编写得很好的C ++)也不太可能做得更好。 As you move to 10.6 and grand central dispatch, the performance difference is going to be even more notable because you'll lose all the parallelization advances that you would get for free otherwise. 当您转向10.6和大型中央调度时,性能差异将更加显着,因为您将失去所有免费获得的并行化进展。 This has nothing to do with ObjC; 这与ObjC无关; Core Image is C. You can call it from C++ all you like. Core Image是C.你可以用C ++来调用它。 I just recommend against custom graphics processing on Mac in any language unless you need portability or you have the expertise necessary to beat Core Image. 我建议不要使用任何语言在Mac上进行自定义图形处理,除非您需要便携性或者您具备击败Core Image所需的专业知识。

You're going to hit one obstacle in the form of what's called "name mangling". 你将以所谓的“名称修改”的形式遇到一个障碍。 C++ stores function names in a way not compatible with Obj-C. C ++以与Obj-C不兼容的方式存储函数名。

Objective-C doesn't implement classes in the same way as C++, so it's not going to like it. Objective-C没有像C ++那样实现类,所以它不会喜欢它。

One way around this is to implement a set of simple C functions which call the C++ functions. 解决此问题的一种方法是实现一组调用C ++函数的简单C函数。 It'll be a good challenge to keep the number of C functions as low as possible! 保持C函数的数量尽可能低是一个很好的挑战! You'll end up with a nice compact interface! 你最终会得到一个漂亮的紧凑界面! :) :)

To declare these functions in a C++ file, you'll need to mark them as C with: 要在C ++文件中声明这些函数,您需要将它们标记为C:

extern "C" int function_name(char *blob,int number, double foo) {...}

This disables the standard name-mangling. 这会禁用标准名称修改。

Build a header file with the prototypes for all these functions that you can share with your objective C code. 使用您可以与目标C代码共享的所有这些函数的原型构建头文件。

You won't be able to pass classes around in the same way (because your ObjC code can't use them), but you'll be able to pass pointers (although you might have to lie about the types a little). 你将无法以相同的方式传递类(因为你的ObjC代码不能使用它们),但是你将能够传递指针(尽管你可能不得不对这些类型撒谎)。

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

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