用Objective-C应用程序链接和使用C ++库 [英] linking and using a C++ library with an Objective-C application

查看:148
本文介绍了用Objective-C应用程序链接和使用C ++库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Objective-C编写一个图形应用程序用于前端,使用C ++编写图形处理和网络通信。我在苹果的网站上阅读,寻找一种方法,将 .dylib .so 与我的C ++代码链接到我的Xcode项目,但似乎没有什么工作。我能够让项目引用它并链接到它,但是当我试图从.dylib调用函数,它说,它不知道我在做什么。有人知道这是怎么回事吗?

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 .dylib or .so with 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 ++,我想学习更多的Objective-C,那么什么更好的方法比使用它们在一起?

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?

谢谢,
Robbie

Thanks, Robbie

推荐答案

你将以所谓的名称调整的形式打出一个障碍。 C ++以与Obj-C不兼容的方式存储函数名。

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.

Objective-C不像C ++那样实现类,所以它不会像

Objective-C doesn't implement classes in the same way as C++, so it's not going to like it.

一个方法是实现一组简单的C函数调用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:

To declare these functions in a C++ file, you'll need to mark them as C with:

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

这将禁用标准名称修改。

This disables the standard name-mangling.

使用原型创建一个头文件,与你的目标C代码。

Build a header file with the prototypes for all these functions that you can share with your objective C code.

你不能以相同的方式传递类(因为你的ObjC代码不能使用它们)将能够传递指针(虽然你可能不得不说谎的类型一点)。

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

这篇关于用Objective-C应用程序链接和使用C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆