你可以使用 C++ Cocoa (Obj-C) 项目中的库?
我正在考虑学习 Objective-C 和 Cocoa,主要是为了使用 Apple 的工具和 GUI。
不过,我也想做一些图形编程; OpenFrameworks 和 Cinder 是两个引起我注意的库,但我们现在是在 C++ 领域。
我来自 Java/Swing/Processing 背景...对 C 系列了解不多。从本机 Cocoa 调用 C 和 C++ 库(如 Cinder 和 OF)的效率如何?
另外,还有一点:这样的解决方案可以在 iPhone 或 iPad 上运行吗?
I'm considering learning Objective-C and Cocoa, mostly in order to use Apple's tools and GUIs.
However, I'd also like to do some graphics programming; OpenFrameworks and Cinder are two libraries which catch my eye, but then we're in C++ land.
I come from a Java/Swing/Processing background... don't know much about the C family. How effectively can you call C and C++ libraries, like Cinder and OF, from native Cocoa?
And, bonus points: would a solution like this work on an iPhone or iPad?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,C++ 非常适合 OS X 和 iOS 程序,并且可以很好地与 Objective-C 配合使用。
更详细地说:
我不会直接代表那些图书馆说话。
更笼统地回答您的问题:C++ 在您的应用程序中就可以了,因为 C、C++、ObjC 和 ObjC++ 是 iOS 应用程序的一流开发语言。
Objective-C++ 允许您在同一个翻译中使用 C、C++ 和 Objective-C。请在需要时随意使用/组合 C++、C 或 ObjC。如果您采用此方法,编译时间将会增加,并且存在一些限制*。否则,支持非常好。甚至 Apple 在其应用程序/库中也使用了大量 C++。
确实。 c++11 对 iOS 和 OS X 的支持目前有点落后。然而,clang 团队很快就增加了对它的支持。如果您想要最新的功能,可能会遇到一些障碍,所以我想说,如果您的项目依赖于前沿的 C++ 和编译器功能,请不要使用它。当然,使用每个版本的最新 clang 功能对程序进行采样以确定它与您的程序的配合程度也没有什么坏处。
更新:此时(2011 年 11 月 8 日)clang 可以处理我扔给它的几乎所有 C++ 2003 代码。与 GCC+LLVM 相比,代码速度和大小确实有所不同。我现在不想放弃其中任何一个,但是这两个编译器都可以很好地使用 C++ 2003,而且 Apple 的 GCC 不支持 c++11,因此如果您想要 c++11 功能,现在是开始测试 clang 的好时机在不久的将来。
*限制:这些是合理的限制 - 您需要的所有功能都存在,但有些事情人们可能期望但不可能。最常见的可能是无法派生不同对象模型的类型。也就是说,您不能合理地不从 objc 类型创建 C++ 类型,但您可以在任一对象类型中自由声明多种类型的 ivars。
In short, C++ is just fine for OS X and iOS programs, and plays with Objective-C quite nicely.
In more detail:
I won't speak for those libraries directly.
To answer your question in more general terms: C++ is just fine in your app, since C, C++, ObjC, and ObjC++ are first class development languages for iOS apps.
Objective-C++ allows you to use C, C++, and Objective-C all in the same translation. Feel free to use/combine C++, C, or ObjC where needed. The compile times will increase, and there are some restrictions* if you take this route. Otherwise, support is very good. Even Apple uses a good amount of C++ in their apps/libs.
Definitely. c++11 support for iOS and OS X is currently a bit behind. However, the clang team's been adding support for it very very quickly. There will likely be some bumps if you want the latest features, so I'd say hold back on bleeding edge C++ and compiler features if your project depends on it. Of course, it does not hurt to sample your program using the latest clang features with each release in order to determine how well it works with your programs.
Update: At this point (Nov 8, 2011) clang can handle nearly all of the C++ 2003 code I throw at it. Code speed and size does vary compared to GCC+LLVM. I would not want to drop either at this point, but both compilers work well for me with C++ 2003, and Apple's GCC will not support c++11, so it's a good time to start testing clang if you want c++11 features in the near future.
*restrictions: these are reasonable restrictions - all the features you need exist, but there are some things that people may expect which are not possible. the most common is likely the inability to derive types of different object models. that is, you cannot reasonably not create a c++ type from an objc type, but you can freely declare ivars of multiple types in either object type.
可以在 iOS 应用程序中同时使用 OpenFrameworks 和 Cinder(请参阅此相关 SO 问题:
It is possible to use both OpenFrameworks and Cinder in iOS applications (see this related SO question: OpenGL ES, OpenFrameworks, Cinder and IOS creative development), though Apple "prefers" developers to use OpenGL.
现在,您可以通过新的 CocoaCinderGL 包装器 在典型的 Cocoa 项目中使用 Cinder。它允许您向应用程序添加多个 NSCustomView,每个 NSCustomView 都运行一个独立的 Cinder“实例”。显然,您可以将任意数量的 NSButton、NSSlider 连接到这些实例。希望有帮助!
As of now you can use Cinder from within a typical Cocoa project via the new CocoaCinderGL wrapper. It lets you add several NSCustomView's to your application, each one running an independent "instance" of Cinder. And obviously you can connect as many NSButtons, NSSliders as you like to those instances. Hope that helps!