简体   繁体   English

适用于iPhone的C vs C ++(Objective-C vs Objective-C ++)

[英]C vs C++ (Objective-C vs Objective-C++) for iPhone

I would like to create a portable library for iPhone, that also could be used for other platforms. 我想为iPhone创建一个可移植的库,也可以用于其他平台。

My question is the fallowing: 我的问题是休息:

Does anyone knows what is the best to be used on the iPhone: Objective-C or Objective-C++? 有谁知道iPhone上最好用的是什么:Objective-C或Objective-C ++? Does it works with C++ the same way as Objective-C with C or not? 它是否适用于C ++与Objective-C与C相同?

Reasons: Objective-C is a superset of C, but Objective-C++ is not a superset of C++. 理由: Objective-C是C的超集,但Objective-C ++ is not C ++的超集。

Thanks in advance! 提前致谢!

UPDATE: What about memory usage, speed in the same implementation of an use case? 更新:在同一个用例的实现中,内存使用情况,速度如何?

UPDATE1: If anyone can provide any more information, he'll be welcome. 更新1:如果有人可以提供更多信息,他将受到欢迎。

If you're writing a portable library, and don't need specific language features for it, why not write it in straight C? 如果您正在编写一个可移植的库,并且不需要特定的语言功能,为什么不直接用C语言编写呢? It can be easily called from C++, C, Objective-C, and many, many other languages. 可以从C ++,C,Objective-C以及许多其他语言轻松调用它。

If you require specific language features that aren't available in C, you should tell us what they are so you can get better advice =) 如果您需要C语言中没有的特定语言功能,您应该告诉我们它们是什么,以便您可以获得更好的建议=)

I believe your information is incorrect. 我相信您的信息不正确。 Objective-C++ is a superset of C++. Objective-C ++ C ++的超集。 Any C++ is legal Objective-C++. 任何C ++都是合法的Objective-C ++。 In addition, you can mix Objective-C and C++ code in an Objective-C++ (usually .mm) file and (with some restrictions) mix Objective-C and C++ class instance variables within an Objective-C++ class. 此外,您可以在Objective-C ++(通常是.mm)文件中混合使用Objective-C和C ++代码,并且(有一些限制)在Objective-C ++类中混合使用Objective-C和C ++类实例变量。 Objective-C++ is particularly useful for interfacing between Objective-C and a C++ library. Objective-C ++对于Objective-C和C ++库之间的接口特别有用。 Write your cross-platform library in C++. 用C ++编写跨平台库。 You can then call it from Objective-C++ within an application. 然后,您可以在应用程序中从Objective-C ++中调用它。 Re-read the Objective-C++ section of the Objective-C language guide for more info. 重新阅读Objective-C语言指南的Objective-C ++ 部分以获取更多信息。

The major downside to using Objective-C++ is increased compile times (Objective-C++ is even worse than C++ and the Clang LLVM compiler doesn't handle Objective-C++ yet). 使用Objective-C ++的主要缺点是编译时间增加(Objective-C ++甚至比C ++差,而Clang LLVM编译器还没有处理Objective-C ++)。 There is no performance difference between Objective-C and Objective-C++ beyond any differences in the code that's called (eg if you're a better C++ dev., your C++ will probably be more efficient than your C library and visa versa). Objective-C和Objective-C ++之间没有任何性能差异超出所调用代码的任何差异(例如,如果你是一个更好的C ++开发者,你的C ++可能会比你的C库更有效,反之亦然)。

They're not really different languages. 他们并不是真正不同的语言。 Objective-C++ is just Objective-C with slightly limited support for including C++ code. Objective-C ++只是Objective-C,对包含C ++代码的支持略有限制。 Objective-C is the standard dialect, but if you need to work with C++, there's no reason not to use it. Objective-C是标准方言,但如果您需要使用C ++,则没有理由不使用它。 AFAIK, the biggest practical difference (aside from allowing use of different libraries) is that Objective-C++ seems to compile a bit slower. AFAIK,最大的实际区别(除了允许使用不同的库)是Objective-C ++似乎编译速度稍慢。 Just be sure to read up on it first if you do decide to go that route, because the merging of C++ and Objective-C is not 100% seamless. 如果您决定采用这种方式,请务必先阅读它,因为C ++和Objective-C的合并不是100%无缝的。

If you are going to use this library on platforms other than the iPhone and Mac OS, you should use C++. 如果您要在iPhone和Mac OS以外的平台上使用此库,则应使用C ++。 If you plan only to port it to Mac OS, then go ahead and use Objective-C. 如果您打算只将其移植到Mac OS,那么继续使用Objective-C。 Objective-C++ is just a way to integrate existing C++ code into Objective-C projects. Objective-C ++只是将现有C ++代码集成到Objective-C项目中的一种方法。 Thus, you would not write a library in it. 因此,您不会在其中编写库。 You might, however, use Objective-C++ to use this library from an iPhone or Mac project. 但是,您可以使用Objective-C ++从iPhone或Mac项目中使用此库。

The truth is you can use C or C++ in Xcode. 事实是你可以在Xcode中使用C或C ++。 The reason to use Objective C for Cocoa and Cocoa Touch (iphone) is because of it's ease of use and how it handles many of the memory issues you would come across on it's own. 使用Objective C for Cocoa和Cocoa Touch(iphone)的原因在于它的易用性以及它如何处理你自己会遇到的许多内存问题。 Objective C and Xcode work like a dream. Objective C和Xcode像梦一样工作。 I would also recommend checking out http://www.kevincallahan.org/software/accessorizer.html Accessorizer is amazing and cuts development time into nothing. 我还建议查看http://www.kevincallahan.org/software/accessorizer.html Accessorizer是惊人的,并将开发时间缩短为零。

Having said that I see no problem in writing functions/methods in C or C++ and using them within a part of your XCode project. 话虽如此,我认为在C或C ++中编写函数/方法并在XCode项目的一部分中使用它们没有问题。 Just do not expect to use Objective C to play as nicely on other platforms. 只是不要指望使用Objective C在其他平台上玩得很好。 Apple and Windows development IDE's are different and Microsoft has a nice habit for not making things as compatible with other environments (not just Apple.) Apple和Windows开发IDE是不同的,微软有一个很好的习惯,就是不要让它与其他环境(不仅仅是Apple)兼容。

I am glad to hear someone out their trying to reuse their code for other projects : ) Too many people forget to create their own libraries these days and I haven't the slightest idea why. 我很高兴听到有人试图将他们的代码重用于其他项目:)现在有太多人忘记创建他们自己的库,我没有丝毫想法。

I hope this helps. 我希望这有帮助。 Cheers, Matthew 干杯,马修

Here is your caveat: Spend the hundred on becoming a developer and launch xcode the IDE you will be working with on the Mac - this would be the best way for you to decide as it is apparent that you do not fully understand how this platform works at all. 以下是您的警告:花费百万成为开发人员并启动xcode,您将在Mac上使用的IDE - 这将是您决定的最佳方式,因为很明显您并不完全了解此平台的工作原理一点都不

Please don't be rude to everyone trying to help you. 请不要对每个试图帮助你的人不礼貌。 This community is built on respect. 这个社区建立在尊重之上。 The people here really actually do help out and care and there are few places like this on the net left. 这里的人实际上确实提供了帮助和关心,网上的这样的地方很少。

Go for C or C++ - I do not see any reason for recommending one over the other without knowing more about your goals. 去C或C ++ - 我不认为有任何理由在不了解你的目标的情况下推荐一个而不是另一个。

In terms of memory usage, C++ and C do demand more disciplined coding. 在内存使用方面,C ++和C确实需要更严格的编码。 In terms of speed, C and C++ will in virtually any case be faster than any ObjC/ObjC++ solution. 就速度而言,C和C ++几乎在任何情况下都比任何ObjC / ObjC ++解决方案都要快。 I do frequently mix my Objective C applications with C and C++ code and was always pleased with the results. 我经常将Objective C应用程序与C和C ++代码混合使用,并且总是对结果感到满意。 As Jonathan correctly stated, ObjectiveC++ is mostly a bridge for using C++ code/libraries within your Objective C application/s. 正如Jonathan所说,ObjectiveC ++主要是在Objective C应用程序中使用C ++代码/库的桥梁。

You can freely use C++ objects from within Objective C++. 您可以在Objective C ++中自由使用C ++对象。 You can however NOT use Objective C/C++ objects within C++. 但是,您不能在C ++中使用Objective C / C ++对象。 So its a one-way-road. 所以它是一条单行道。 To overcome any limitations imposed by this restriction, simply wrap your C++ objects with Objective C++ classes where needed. 要克服此限制所施加的任何限制,只需在需要时使用Objective C ++类包装C ++对象。

"Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++." “Objective-C是C的严格超集,而Objective-C ++是C ++的严格超集。” - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html

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

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