xcode 将 C++ 库与 Objective-C 应用程序链接并使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1233381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:43:02  来源:igfitidea点击:

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

c++objective-cxcodelinkerdylib

提问by Robbie

I'm writing a graphical application using Objective-C for the front end and C++ for the graphics processing and network communication. I read around on Apple's site looking for a way to link either a .dylibor .sowith my C++ code in it to my Xcode project, but nothing seemed to work. 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. Does anyone know what is going on here?

我正在编写一个图形应用程序,前端使用 Objective-C,使用 C++ 进行图形处理和网络通信。我在 Apple 的网站上四处阅读,寻找一种将.dylib.so与其中的 C++ 代码链接到我的 Xcode 项目的方法,但似乎没有任何效果。我能够让项目引用它并链接它,但是当我尝试从那个 .dylib 调用函数时,它说它不知道我想要做什么。有谁知道这里发生了什么?

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. 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?

我知道 Objective-C 拥有我做图形和网络所需的所有库,但我只是喜欢这样做。我有一段时间没有做太多的 C++,我想学习更多的 Objective-C,那么有什么比一起使用它们更好的方法呢?

Thanks, Robbie

谢谢,罗比

回答by Rob Napier

Most of the projects I work on have an ObjC frontend and C++ backend. 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. Using opaque references (which is a very fancy way of saying void*), you can actually hand around C++ objects in ObjC and vice versa. I have some sample codethat may be helpful.

我从事的大多数项目都有一个 ObjC 前端和 C++ 后端。如果您专门处理函数,那么 Dave Gamble 的名称错误修复是正确的,但如果您处理更复杂的情况,需要同时处理 ObjC 和 C++ 对象,最好的办法是包装 C++ 对象在 ObjC 对象中。使用不透明引用(这是一种非常奇特的说法void*),您实际上可以在 ObjC 中传递 C++ 对象,反之亦然。我有一些可能有用的示例代码

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. 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). 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. This has nothing to do with ObjC; Core Image is C. You can call it from C++ all you like. 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.

也就是说,对于图形,您可能会在使用自定义 C++ 而不是使用 Core Image 和相关框架时严重影响性能。Core Image 和其他图形框架针对 Mac 进行了高度优化,您不太可能使用手卷的 C++(甚至不是专门为 Mac 编写的非常好的 C++)做得更好。当您转向 10.6 和大中央调度时,性能差异将更加显着,因为您将失去原本可以免费获得的所有并行化进步。这与 ObjC 无关;Core Image 是 C。你可以随心所欲地从 C++ 中调用它。我只是建议不要在 Mac 上使用任何语言进行自定义图形处理,除非您需要可移植性或者您拥有击败 Core Image 所需的专业知识。

回答by Dave Gamble

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. It'll be a good challenge to keep the number of C functions as low as possible! You'll end up with a nice compact interface! :)

解决此问题的一种方法是实现一组调用 C++ 函数的简单 C 函数。保持尽可能少的 C 函数数量将是一个很好的挑战!你最终会得到一个漂亮紧凑的界面!:)

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 代码无法使用它们),但您将能够传递指针(尽管您可能不得不对类型撒一点谎)。