用 C++ 编写 Mac OS X 应用程序的完整解决方案

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

Complete solution for writing Mac OS X application in C++

c++xcodemacoside

提问by Marcin Gil

I am looking for alternatives for my current box and Mac OS X seems very appealing.

我正在为我当前的盒子寻找替代品,而 Mac OS X 似乎非常吸引人。

My main area of interest is C++ programming. Currently I'm using Eclipse + CDT and g++ for creating my software; sometimes it is KDevelop.

我的主要兴趣领域是 C++ 编程。目前我正在使用 Eclipse + CDT 和 g++ 来创建我的软件;有时是 KDevelop。

I know that primary IDE for Mac is Xcode and primary language is Objective-C. I would like to avoid learning Objective-C if at all possible. I've also heard/read that there are some issues in accessing Mac OS X APIs from C++.

我知道 Mac 的主要 IDE 是 Xcode,主要语言是 Objective-C。如果可能的话,我想避免学习 Objective-C。我还听说/读过从 C++ 访问 Mac OS X API 存在一些问题。

Hence my question: what is the complete solution for developing/debugging/testing C++ applications that access all aspects of hardware (UI, sound, video/accelerated video, etc.) for Mac OS X?

因此我的问题是:用于开发/调试/测试访问 Mac OS X 硬件(UI、声音、视频/加速视频等)所有方面的 C++ 应用程序的完整解决方案是什么?

Edit: how does Xcode compare to the Eclipse+CDT combo? If this comparison is at all possible...

编辑:Xcode 与 Eclipse+CDT 组合相比如何?如果这种比较是可能的......

采纳答案by Pablo Santa Cruz

If you want to use C++ instead of Objective-C, and still want to avoid an intermediate layer of libraries (such as QT), you can use Carbon.

如果您想使用 C++ 而不是 Objective-C,并且仍然想避免库的中间层(例如 QT),您可以使用Carbon

I would use XCode instead of Eclipse simply because Eclipse is way slower when dealing with hardcore C/C++ programming (compiling, debugging, testing).

我会使用 XCode 而不是 Eclipse,因为 Eclipse 在处理硬核 C/C++ 编程(编译、调试、测试)时要慢得多。

When I first started to program in Mac OS X, I was in the same page you are now. I thought it was better to stick to the language I knew (C++) and use an older library (Carbon). For some reason I don't remember now, I forced myself into Cocoa (Objective-C). Looking back, I think it was a good thing because:

当我第一次开始在 Mac OS X 中编程时,我与您现在处于同一页面。我认为最好坚持使用我知道的语言(C++)并使用较旧的库(Carbon)。出于某种我现在不记得的原因,我强迫自己进入 Cocoa(Objective-C)。回顾过去,我认为这是一件好事,因为:

  1. Objective-C is not fundamentally different to C++
  2. Cocoa is better, faster and simpler than Carbon
  3. iPhone Dev is exclusively Cocoa (Carbon is not supported)
  1. Objective-C 与 C++ 没有根本的不同
  2. Cocoa 比 Carbon 更好、更快、更简单
  3. iPhone Dev 完全是 Cocoa(不支持 Carbon)

回答by Stephan Eggermont

You should notavoid learning Objective-C and Cocoa. It should in fact be the first thing you do. Unless you have a solid background in Smalltalk, you're unlikely to have been exposed to a large (set of) framework that is as well-designed.

你应该避免学习Objective-C和Cocoa。事实上,这应该是你做的第一件事。除非您在 Smalltalk 方面有扎实的背景,否则您不太可能接触过设计良好的大型(一组)框架。

回答by Genericrich

Objective-C is not that hard. If you know C++ it will take you a day or three of applied effort to learn the conventions.

Objective-C 并不难。如果您了解 C++,则需要花一到三天的时间来学习这些约定。

The Cocoa APIs and conventions take longer, but those come in time.

Cocoa API 和约定需要更长的时间,但这些都是及时的。

Good luck!

祝你好运!

回答by Ferruccio

I've been playing around using Objective-C++ to write the UI, making it easier to use C++ to write the application back-end. It seems to work pretty well for me. Objective-C & Objective-C++ are pretty easy to pick up and this lets me keep using my hard-won C++ knowledge.

我一直在尝试使用 Objective-C++ 编写 UI,从而更容易使用 C++ 编写应用程序后端。这对我来说似乎很有效。Objective-C 和 Objective-C++ 很容易上手,这让我可以继续使用我来之不易的 C++ 知识。

回答by Timo Geusch

Unless you're looking at using an intermediate layer like QT, I think you're pretty much stuck with Objective C to interface with parts of the API like the GUI as it is their native language.

除非您正在考虑使用像 QT 这样的中间层,否则我认为您几乎无法使用 Objective C 来与 API 的一部分(如 GUI)进行交互,因为它是它们的母语。

That said, it has been mentioned that you can easily mix Objective-C code and C++ code so you may still be able to write the majority of your application in C++.

也就是说,已经提到您可以轻松地混合 Objective-C 代码和 C++ 代码,因此您仍然可以用 C++ 编写大部分应用程序。

I'm mainly a C++ programmer but I'm always dabbling in other languages and I've had a play with XCode (yes, I've got a Mac). I liked it, it's a bit different but as a way of nailing together an OS X GUI it's superb and the rest is between you and your favourite editor if you don't like XCode that much.

我主要是一个 C++ 程序员,但我总是涉足其他语言,我玩过 XCode(是的,我有一台 Mac)。我喜欢它,它有点不同,但作为将 OS X GUI 固定在一起的一种方式,它非常棒,如果您不太喜欢 XCode,剩下的就在您和您最喜欢的编辑器之间。

回答by jkyle

C++ works very well with Xcode. If you want cocoa, you can learn a minimal (and I mean really minimal) amount of Obj-C for the gui layer and have the bulk of your code in C++.

C++ 与 Xcode 配合得很好。如果您想要可可,您可以为 gui 层学习最少(我的意思是非常少)的 Obj-C,并使用 C++ 编写大部分代码。

Another option is using one of the scripting language frameworks for hooking into the Cocoa API (such as PyObjc) and the bulk of your logic in C++.

另一种选择是使用一种脚本语言框架来连接 Cocoa API(例如 PyObjc)和 C++ 中的大部分逻辑。

The third and final option is using a cross platform gui kit such as Qt4. If there's a desire to make your libraries cross platform, this would be the route to go.

第三个也是最后一个选项是使用跨平台 gui 工具包,例如 Qt4。如果希望让您的库跨平台,这将是您要走的路。

I would recommend against going with Carbon. It's definitely a dying beast. You could also use combinations of the above providing a library with your C++ code then creating gui specific UI's that link them them.

我建议不要使用 Carbon。这绝对是一头垂死的野兽。您还可以使用上述方法的组合,为您的 C++ 代码提供一个库,然后创建将它们链接起来的特定于 gui 的 UI。

回答by Alex Fort

It's no problem using gcc or g++ from OS X. The XCode install gives you these compilers anyway, and if you really wanted, you could load up an x server instance and get KDE working. I like the XCode editor myself though. Eclipse also works with OS X, so you should be able to use your current environment.

使用 OS X 中的 gcc 或 g++ 没有问题。无论如何,XCode 安装都会为您提供这些编译器,如果您真的需要,您可以加载一个 x 服务器实例并使 KDE 工作。不过我自己喜欢 XCode 编辑器。Eclipse 也适用于 OS X,因此您应该能够使用您当前的环境。

I've used an OS X box for cross-compiling C code from PPC arch to ARM arch before, with no problems. It's just another *nix style system, so all the tools you are familiar with will already exist, and if they don't, just use finkor macportsto get them yourself.

我以前使用 OS X 框将 C 代码从 PPC arch 交叉编译到 ARM arch,没有任何问题。它只是另一个 *nix 风格的系统,所以您熟悉的所有工具都已经存在,如果没有,只需使用finkmacports自己获取它们。

Also, if you really wanted to use C++ for developing GUI OS X apps, you could always use Carbon instead of Cocoa. It's a perfectly viable choice, only slightly deprecated.

此外,如果您真的想使用 C++ 来开发 GUI OS X 应用程序,您始终可以使用 Carbon 而不是 Cocoa。这是一个完全可行的选择,只是略有弃用。

回答by jdkoftinoff

Carbon on Mac OS X is effectively deprecated - it does not and will not support 64 bit apps.

Mac OS X 上的 Carbon 已被有效弃用 - 它不支持也不会支持 64 位应用程序。

If you really really want to use c++, use Qt Creator

如果你真的很想用c++,就用Qt Creator

Which is now free and the trolltech SDK includes Qt 4.5, which while it is a c++ framework it internally uses Objective-C to interact with MAC OS X, so then you are not limited by Carbon.

现在是免费的,trolltech SDK 包括 Qt 4.5,虽然它是一个 C++ 框架,但它内部使用 Objective-C 与 MAC OS X 交互,因此您不受 Carbon 的限制。

The downside is that the trolltech libraries are not standard on the mac, you must provide them with your app.

缺点是 trolltech 库不是 mac 上的标准库,您必须将它们与您的应用程序一起提供。

回答by John Fricker

I agree with Pablo Santa Cruz and would like to add that it's trivial to mix Objective-C with C++ in the same app (and the same source file). This is referred to as Objective-C++ and discussed in this article.

我同意 Pablo Santa Cruz 的观点,并想补充一点,在同一个应用程序(和同一个源文件)中混合 Objective-C 和 C++ 是微不足道的。这被称为 Objective-C++ 并在本文中进行了讨论。

XCode compiles with GCC so you have Objective C, C, and C++ ready at your finger tips.

XCode 使用 GCC 进行编译,因此您可以轻松使用 Objective C、C 和 C++。

回答by Emiel

For some small hobby projects, and an iPhone app I'm making, I'm using a mix of objective C++ and 'real' C++. XCode supports the mixing of both languages fairly well. I use objective C/objective C++ for the user interface and as a thin layer between the OSX frameworks and my own code. So far, this has turned out to be a great way of working.

对于一些小型爱好项目和我正在制作的 iPhone 应用程序,我混合使用了客观 C++ 和“真正的”C++。XCode 很好地支持两种语言的混合。我将目标 C/目标 C++ 用于用户界面,并作为 OSX 框架和我自己的代码之间的薄层。到目前为止,这已经证明是一种很好的工作方式。