xcode 将 C++ 库集成到 iPhone 应用程序中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/308673/
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:25:51  来源:igfitidea点击:

Integrate C++ library into iPhone app

c++iosxcode

提问by Khushi

Can I integrate my exsisting C++ library in an iPhone application?

我可以将现有的 C++ 库集成到 iPhone 应用程序中吗?

回答by Nik Reiman

While it's true that you can't mix C++ classes in Objective-C, you can mix it with Objective-C++, which I believe is also supported in the iPhone. Integrating C++ code with an iPhone app shouldn't be an issue, but if you want to stay on the safe side, then you should build your code as a library and then link to it from your iPhone app; that way you don't need to worry about mixing Obj-C code and C++ classes.

虽然你不能在 Objective-C 中混合 C++ 类,但你可以将它与 Objective-C++ 混合,我相信 iPhone 也支持它。将 C++ 代码与 iPhone 应用程序集成应该不是问题,但如果您想保持安全,那么您应该将代码构建为一个库,然后从您的 iPhone 应用程序链接到它;这样你就不必担心混合 Obj-C 代码和 C++ 类。

As for Apple's approval, nobody can give you a definitive answer as to what may or may not qualify your app for inclusion in the store, since Apple is rather closed about the whole process. However, it's pretty clear by now that it's certain types of applications aren't going to make the cut; namely:

至于苹果的批准,没有人可以给你一个明确的答案,以确定你的应用程序是否有资格进入商店,因为苹果对整个过程相当封闭。但是,现在很明显,某些类型的应用程序不会被淘汰。即:

  • Apps that "abuse" or twist the iPhone SDK in ways Apple doesn't like. IE, setting the brightness of the device
  • Using too much bandwidth or system resources (draining battery life unnecessarily)
  • Duplicating Apple's functionality somewhere (ie, making a mail client, web browser, etc.)
  • Abusing the pricing tiers and schemes
  • 以 Apple 不喜欢的方式“滥用”或扭曲 iPhone SDK 的应用程序。IE,设置设备的亮度
  • 使用过多的带宽或系统资源(不必要地消耗电池寿命)
  • 在某处复制 Apple 的功能(即制作邮件客户端、网络浏览器等)
  • 滥用定价等级和方案

Actually, it's better to just google for examples of banned apps to get a better feel for it. But the bottom line is, it's not howyour app does something, it's whatyour app is doing which will lead to it being banned.

实际上,最好只是在谷歌上搜索被禁止的应用程序的示例,以更好地了解它。但底线是,它不是如何您的应用程序做一些事情,这是什么您的应用程序在做,这将导致它被禁止。

回答by Pieter

Objective C++ is well defined and supported by Apple's gcc.

Apple 的 gcc 对 Objective C++ 进行了很好的定义和支持。

But you can't mix C++ and objective C classes, which makes this rather messy pretty fast. I don't think there are many projects in the wild which mix C++ and Objective C to any serious extent.

但是你不能混合 C++ 和目标 C 类,这使得这相当快。我不认为有很多项目将 C++ 和 Objective C 混合到任何严重的程度。

This highly depends on your library, but if (realistically) feasible I'd wrap the C++ inside a C library, using extern "C" entrypoints for all real functionality, and link with that from inside your objective C code for your iphone app.

这在很大程度上取决于您的库,但如果(实际上)可行,我会将 C++ 包装在 C 库中,对所有实际功能使用 extern "C" 入口点,并从您的 iphone 应用程序的目标 C 代码中链接到它。

回答by Dolphin

I have just gotten started with iPhone development, but I have been able to successfully use some existing C++ libs without any changes. I brought over some OpenGL classes that I have already written and they work just fine since the gl libs on the iPhone are all C and not Obj-C. I am, for example, loading a texture into a GLubyte array using Obj-C to get the resources, size, etc. then passing those values to my C++ code which does the gl calls to create the texture. The same applies for loading geometry.

我刚刚开始使用 iPhone 开发,但我已经能够成功地使用一些现有的 C++ 库而无需任何更改。我带来了一些我已经写过的 OpenGL 类,它们工作得很好,因为 iPhone 上的 gl 库都是 C 而不是 Obj-C。例如,我使用 Obj-C 将纹理加载到 GLubyte 数组中以获取资源、大小等,然后将这些值传递给我的 C++ 代码,该代码执行 gl 调用以创建纹理。这同样适用于加载几何体。

Just change your .m files to .mm and everything just seems to work. Objective C++ classes can have members that are C++ classes (though I don't think the other way applies, which could be a problem).

只需将您的 .m 文件更改为 .mm ,一切似乎都可以正常工作。目标 C++ 类可以具有 C++ 类的成员(尽管我认为其他方式不适用,这可能是一个问题)。