简体   繁体   English

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

[英]Complete solution for writing Mac OS X application in C++

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.我的主要兴趣领域是 C++ 编程。 Currently I'm using Eclipse + CDT and g++ for creating my software;目前我正在使用 Eclipse + CDT 和 g++ 来创建我的软件; sometimes it is KDevelop.有时它是KDevelop。

I know that primary IDE for Mac is Xcode and primary language is Objective-C.我知道 Mac 的主要 IDE 是 Xcode 主要语言是 Objective-C。 I would like to avoid learning Objective-C if at all possible.如果可能的话,我想避免学习 Objective-C。 I've also heard/read that there are some issues in accessing Mac OS X APIs from 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?因此我的问题是:开发/调试/测试 C++ 应用程序的完整解决方案是什么,这些应用程序可以访问 Mac OS X 的硬件(UI、声音、视频/加速视频等)的所有方面?

Edit: how does Xcode compare to the Eclipse+CDT combo?编辑:Xcode 与 Eclipse+CDT 组合相比如何? If this comparison is at all possible...如果这种比较是可能的......

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.当我第一次开始在 Mac OS X 中编程时,我和你现在处于同一页面。 I thought it was better to stick to the language I knew (C++) and use an older library (Carbon).我认为最好坚持使用我知道的语言(C++)并使用较旧的库(Carbon)。 For some reason I don't remember now, I forced myself into Cocoa (Objective-C).出于某种我现在不记得的原因,我强迫自己进入 Cocoa (Objective-C)。 Looking back, I think it was a good thing because:回想起来,我认为这是一件好事,因为:

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

You should not avoid learning Objective-C and Cocoa.应该避免学习 Objective-C 和 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.除非您在 Smalltalk 方面有扎实的背景,否则您不太可能接触过设计良好的大型(一组)框架。

Objective-C is not that hard. Objective-C 并不难。 If you know C++ it will take you a day or three of applied effort to learn the conventions.如果您知道 C++,那么您将需要一到三天的时间来学习这些约定。

The Cocoa APIs and conventions take longer, but those come in time. Cocoa API 和约定需要更长的时间,但这些都是及时的。

Good luck!祝你好运!

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).我主要是一名 C++ 程序员,但我总是涉足其他语言,并且我玩过 XCode(是的,我有一台 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.我喜欢它,它有点不同,但作为一种将 OS X GUI 组合在一起的方式,它非常棒,如果你不太喜欢 XCode,rest 就在你和你最喜欢的编辑器之间。

I've been playing around using Objective-C++ to write the UI, making it easier to use C++ to write the application back-end.我一直在尝试使用 Objective-C++ 编写 UI,从而更容易使用 C++ 编写应用程序后端。 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 和 Objective-C++ 很容易上手,这让我可以继续使用我来之不易的 C++ 知识。

C++ works very well with Xcode. C++ 与 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++.如果您想要 cocoa,您可以为 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)和 ZF6F87C9FDCF8B3C3F07F93F1EE8712C9 中的大部分逻辑。

The third and final option is using a cross platform gui kit such as Qt4.第三个也是最后一个选项是使用跨平台的 gui 工具包,例如 Qt4。 If there's a desire to make your libraries cross platform, this would be the route to go.如果希望使您的库跨平台,这将是通往 go 的途径。

I would recommend against going with Carbon.我建议不要使用 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.您还可以使用上述组合,为您的 C++ 代码提供一个库,然后创建链接它们的 gui 特定 UI。

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.使用 OS X 中的 gcc 或 g++ 没有问题。 XCode 安装无论如何都会为您提供这些编译器,如果您真的想要,您可以加载一个服务器并运行 x。 I like the XCode editor myself though.不过,我自己喜欢 XCode 编辑器。 Eclipse also works with OS X, so you should be able to use your current environment. 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.我之前使用 OS X 机器将 C 代码从 PPC 架构交叉编译到 ARM 架构之前,没有任何问题。 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 fink or macports to get them yourself.它只是另一个 *nix 风格的系统,所以你熟悉的所有工具都已经存在,如果不存在,只需使用finkmacports自己获取它们。

Also, if you really wanted to use C++ for developing GUI OS X apps, you could always use Carbon instead of Cocoa.此外,如果你真的想使用 C++ 来开发 GUI OS X 应用程序,你总是可以使用 Carbon 而不是 Cocoa。 It's a perfectly viable choice, only slightly deprecated.这是一个完全可行的选择,只是略微被弃用。

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

The downside is that the trolltech libraries are not standard on the mac, you must provide them with your app.缺点是 trolltech 库在 mac 上不是标准的,您必须在您的应用程序中提供它们。

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).我同意 Pablo Santa Cruz 的观点,并想补充一点,在同一个应用程序(和同一个源文件)中混合 Objective-C 和 C++ 是微不足道的。 This is referred to as Objective-C++ and discussed in this article .这被称为 Objective-C++ 并在本文中讨论。

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

The development environment that I am using right now for development in the Mac is QTCreator which is a free IDE for development in QT with gcc as compiler.我现在在 Mac 中用于开发的开发环境是 QTCreator,它是一个免费的 IDE,用于在 QT 中开发,gcc 作为编译器。 It gives all the features I need and it is cross platform (one requirement of the application I am developing).它提供了我需要的所有功能,而且它是跨平台的(我正在开发的应用程序的一项要求)。

I have to say that I've been fairly impressed.我不得不说我印象非常深刻。 I didn't have high hopes for such a focused (specifically designed for QT) and recent IDE.我对这种专注(专为 QT 设计)和最近的 IDE 并没有寄予厚望。 The environment is easy but powerful (including GUI editing) and has the better code completion I've ever seen.该环境简单但功能强大(包括 GUI 编辑),并且具有我见过的更好的代码完成。 It is a breeze programming in this environment and don't miss a bit Visual Studio.在这种环境下编程是一件轻而易举的事,不要错过一点 Visual Studio。

Really worth a try.真的值得一试。

I haven't used it on OSX myself (I can recommend it on Windows), but the Code::Blocks C++ IDE is implemented using, and supports, the cross-platform wxWidgets GUI library.我自己没有在 OSX 上使用过它(我可以在 Windows 上推荐它),但是 Code::Blocks C++ IDE 是使用并支持跨平台的 wxWidgets GUI 库实现的。 It is supported on OSX - see this page .它在 OSX 上受支持 - 请参阅此页面

I would advice you to stick with whichever IDE you are most comfortable with.我建议您坚持使用您最熟悉的 IDE。 Personally, I hated Xcode the first time I used it and refused to give up Eclipse.就个人而言,我第一次使用它时讨厌 Xcode 并拒绝放弃 Eclipse。 After a few days, I warmed up to Xcode and don't mind using it from time to time.几天后,我热身到 Xcode 并且不介意不时使用它。 However, I still use Eclipse whenever possible.但是,我仍然尽可能使用 Eclipse。

Just remember that you cannot write Cocoa applications using only C++, you need to dive into Obj-C for that.请记住,您不能仅使用 C++ 编写 Cocoa 应用程序,您需要为此深入研究 Obj-C。 Carbon applications don't quite look or behave like a Cocoa app. Carbon 应用程序的外观或行为不像 Cocoa 应用程序。 Most people won't really mind but it's something to think about.大多数人不会真的介意,但这是值得考虑的事情。 I jumped into Obj-C from java/C# without any issues.我从 java/C# 跳入 Obj-C 没有任何问题。 The language takes a little getting used to but it's not very difficult.这种语言需要一点时间来适应,但并不难。

If you really want to use C++ on Mac OS X, use something like SDL / wxWidgets / Qt .如果您真的想在 Mac OS X 上使用 C++,请使用SDL / wxWidgets / Qt 之类的东西。

There's the option of combining Objective-C with C++, called Objective-C++ .可以选择将 Objective-C 与 C++ 结合起来,称为Objective-C++ You could use it for all your model-stuff and just use Objective-C for the stuff, that isn't possible without the Foundation-framework.您可以将它用于所有模型材料,只需将 Objective-C 用于这些材料,如果没有 Foundation 框架,这是不可能的。

As a last option, Carbon comes to mind.作为最后一个选择,我想到了Carbon This is a native framework that was used in the pre-OSX times but runs on Mac OS X. The problem is, it's not supported for newer technologies and I think the next release of OS X, Snow Leopard, isn't going to support it at all.这是一个在前 OSX 时代使用的本机框架,但在 Mac OS X 上运行。问题是,它不支持更新的技术,我认为 OS X 的下一个版本 Snow Leopard 不会支持它根本没有。

Apple says:苹果 说:

Carbon enables C/C++ developers to take advantage of Mac OS X features, including the Quartz 2D graphics library, an efficient event-handling mechanism, and multiprocessing support. Carbon 使 C/C++ 开发人员能够利用 Mac OS X 的功能,包括 Quartz 2D 图形库、高效的事件处理机制和多处理支持。 In addition, other industry-standard C/C++ APIs are available to Carbon developers, providing access to such services as the OpenGL drawing system, the Mach microkernel, and BSD operating-system services.此外,Carbon 开发人员还可以使用其他行业标准的 C/C++ API,提供对 OpenGL 绘图系统、Mach 微内核和 BSD 操作系统服务等服务的访问。

Don't expect Carbon applications to look 100% native and it's much more complicated for programming than Cocoa.不要期望 Carbon 应用程序看起来是 100% 原生的,而且它的编程比 Cocoa 复杂得多。

However, I recommend learning Objective-C , it's not hard and there are lots of powerful frameworks like CoreData, CoreGraphics and the like.不过,我推荐学习 Objective-C ,它并不难,而且有很多强大的框架,如 CoreData、CoreGraphics 等。

For some small hobby projects, and an iPhone app I'm making, I'm using a mix of objective C++ and 'real' C++.对于一些小的爱好项目和我正在制作的 iPhone 应用程序,我使用了客观 C++ 和“真实”C++ 的组合。 XCode supports the mixing of both languages fairly well. XCode 很好地支持两种语言的混合。 I use objective C/objective C++ for the user interface and as a thin layer between the OSX frameworks and my own code.我使用 Objective C/objective C++ 作为用户界面,并作为 OSX 框架和我自己的代码之间的薄层。 So far, this has turned out to be a great way of working.到目前为止,这已被证明是一种很好的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM