在 iOS 和 Mac OS X 应用程序中使用 C++

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

Utilizing C++ in iOS and Mac OS X applications

c++objective-cioscocoa

提问by Kris

I'm somewhat moderate in my C++ knowledge but know next to nothing regarding Objective-C. I am planning (and even starting to) learn Objective-C so I can attempt to write a few iOS and even Mac OS X applications but I'm very curious about something I haven't been able to find yet.

我的 C++ 知识有点中庸,但对 Objective-C 几乎一无所知。我正在计划(甚至开始)学习 Objective-C,这样我就可以尝试编写一些 iOS 甚至 Mac OS X 应用程序,但我对尚未找到的东西很好奇。

Is it possible to write an iOS and / or a Mac OS X application entirely with C++ including the UI components? If it isn't possible to do it entirely in C++ then to what degree is it possible to use mostly C++?

是否可以完全使用 C++ 编写 iOS 和/或 Mac OS X 应用程序,包括 UI 组件?如果不可能完全用 C++ 来完成,那么主要使用 C++ 的程度有多大?

I haven't seen any examples that demonstrate either of these items. I am planning on writing a few mobile applications for iOS and Android and, where possible, I'd like to contain most of my logic inside of C++ code for maximum portability. I already know I can do this but I am unsure of the degree.

我还没有看到任何演示这些项目的示例。我计划为 iOS 和 Android 编写一些移动应用程序,并且在可能的情况下,我希望将我的大部分逻辑包含在 C++ 代码中,以实现最大的可移植性。我已经知道我可以做到这一点,但我不确定学位。

回答by Macmade

Short answer, yes.

简短的回答,是的。

For pure C++, you should take a look at the QT framework.
Otherwise, you'll have hard time for the UI part.

对于纯 C++,你应该看看QT 框架
否则,您将很难处理 UI 部分。

But also remember that Objective-C can be mixed with C++.
That's called Objective-C++(.mm files).

但也要记住,Objective-C 可以与 C++ 混合使用。
这就是所谓的Objective-C++(.mm 文件)。

You can then write code that mix C++ and Objective-C code. With this, you can have the UI parts in Objective-C (may be better, as it will use the native frameworks for the UI components), and the other things in C++.

然后,您可以编写混合 C++ 和 Objective-C 代码的代码。有了这个,你可以在 Objective-C 中拥有 UI 部分(可能更好,因为它将使用 UI 组件的本机框架),而在 C++ 中拥有其他东西。

If you've decided to learn Objective-C, but still want to code in C++ for some parts, I would recommend this approach, instead of pure C++.

如果您决定学习 Objective-C,但仍想用 C++ 编写某些部分的代码,我会推荐这种方法,而不是纯 C++。

On iOS, this is also the only way. While you can code in C++, you have to use Objective-C for the UI part.

iOS 上,这也是唯一的方法。虽然您可以使用 C++ 进行编码,但您必须将 Objective-C 用于 UI 部分。

EDIT

编辑

Here are a few links to get started with Objective-C++:

下面是一些开始使用 Objective-C++ 的链接:

回答by Padavan

Look this question

看这个问题

I don't know about Mac OS, but in IOS applications you can use C++ in logic but you have to write user interface on Objective-C.

我不了解 Mac OS,但在 IOS 应用程序中,您可以在逻辑中使用 C++,但您必须在 Objective-C 上编写用户界面。

回答by Gob00st

Is it possible to write an iOS and / or a Mac OS X application entirely with C++ including the UI components? If it isn't possible to do it entirely in C++ then to what degree is it possible to use mostly C++?

是否可以完全使用 C++ 编写 iOS 和/或 Mac OS X 应用程序,包括 UI 组件?如果不可能完全用 C++ 来完成,那么主要使用 C++ 的程度有多大?

I think it is possible to use pure c++ if you want to depend on some 3rd part lib, then yes.

我认为如果您想依赖某些第三部分库,可以使用纯 C++,然后可以。

Without that 3rd part lib you have to write all the UI using objective-c the rest you can use c++ as much as you like.

如果没有第三部分库,您必须使用objective-c 编写所有UI,其余的您可以随心所欲地使用c++。

回答by Payal

Changes required in build settings to use C++ files in iOS project under "Apple LLVM compiler 4.2 - Language" Option

在“Apple LLVM 编译器 4.2 - 语言”选项下的 iOS 项目中使用 C++ 文件所需的构建设置更改

C++ Language Dialect: Compiler Default C++ Standard Library: Compiler Default

C++ 语言方言:编译器默认 C++ 标准库:编译器默认

回答by barney

You could use Cocos2d-x engine and write pure C++ for your iOS app, including UI (cocos has basic ui classes like buttons, scroll views, table views, etc - but you need to develop your own Controllers, and Models using various C++ libraries like Hiberlite, or raw SQLite etc)

您可以使用 Cocos2d-x 引擎并为您的 iOS 应用程序编写纯 C++,包括 UI(cocos 具有基本的 ui 类,如按钮、滚动视图、表格视图等 - 但您需要使用各种 C++ 库开发自己的控制器和模型如 Hiberlite 或原始 SQLite 等)

Cocos has its own interface with iOS/MacOS (a few basic .mm objective-c classes to setup Application run cycle - like app delegate, or runloop and drawing cycle)

Cocos 有自己的 iOS/MacOS 接口(一些基本的 .mm 目标 c 类来设置应用程序运行周期 - 如应用程序委托,或运行循环和绘图周期)

So you just write your user code with C++

所以你只需用 C++ 编写你的用户代码