简体   繁体   English

C ++程序可以在Mac OS上运行吗?

[英]Can a C++ program run on Mac OS?

I have watched a video in which one of the director of scs software (who makes a EURO TRUCK SIMULATOR game) saying that the game is written entirely in C++, yet the game runs on Windows and Mac OS as well. 我观看了一段视频,其中有一个scs软件主任(他制作了EURO TRUCK SIMULATOR游戏)说该游戏完全用C ++编写,但是该游戏也可以在Windows和Mac OS上运行。 How could they achieve it? 他们如何实现? Is it possible to run a C++ program on MAC OS? 是否可以在MAC OS上运行C ++程序?

Yes. 是。

...OK, stack overflow wants at least 30 characters. ...好的,堆栈溢出至少需要30个字符。 OS X comes with the C++ compiler clang++ , which is built on top of llvm . OS X带有C ++编译器clang++ ,该编译器基于llvm构建。 In fact, Apple stopped supplying a port of gcc some time ago, so developers either have to build it themselves or adapt to clang . 实际上,苹果早在一段时间前就停止提供gcc端口,因此开发人员必须自己构建它或适应clang According to Wikipedia, Apple itself develops clang . 根据Wikipedia的说法,苹果公司自己开发clang

可以在终端中运行gcc或使用Xcode对其进行编译,但是您不能再使用Cocoa,但Mac可以使用Qt之类的替代方法。

Sure, there are C++ compilers for Mac OS X as well. 当然,也有适用于Mac OS X的C ++编译器。 One is even included in xcode (the standard development environment on a Mac). xcode(Mac上的标准开发环境)甚至包含一个。

But that does not mean, that you can compile just any C++ program on a Mac. 但这并不意味着您可以在Mac上仅编译任何C ++程序。 It has to be designed to use libraries, that are available on both platforms (instead of MFC on Windows or Cocoa on the Mac). 它必须设计为使用在两个平台上都可以使用的库(而不是Windows上的MFC或Mac上的Cocoa)。 This is possible for example by using wxwidgets or QT. 例如,这可以通过使用wxwidgets或QT来实现。 With these two libraries it is even possible to compile the program for Unix platforms like Linux as well. 使用这两个库,甚至还可以为Linux之类的Unix平台编译程序。

Regardless of language a multi-platform program relies on some basic support parts that are implemented differently on each platform. 不管使用哪种语言,多平台程序都依赖于一些基本支持部分,这​​些部分在每个平台上都以不同的方式实现。 With pure standard C++ this is the standard library and the runtime support, but in practice a game, say, will also rely on 3 rd party libraries with platform specific implementations. 用纯标准C ++,这是标准库和运行时支持,但在实践中一场比赛,比方说,也将依靠3个平台的具体实现第三方库。 So given that the platform itself is compatible with the basic platform assumptions of the C++ language, and the Mac is, achieving such portability is conceptually very straightforward, although difficult to get perfect. 因此,假设平台本身与C ++语言的基本平台假设以及Mac都兼容,则实现这种可移植性从概念上讲非常简单,尽管很难做到完美。

As an example of a platform that's not entirely compatible with C++, Microsoft's .NET virtual machine is apparently so at odds with the ideas of C++ that Microsoft has found it necessary to define two different non-standard variants of C++ for it, and offers no way to produce GUI programs for it in pure standard C++. 作为不完全与C ++兼容的平台的示例,Microsoft的.NET虚拟机显然与C ++的想法不一致,因为Microsoft发现有必要为其定义两个不同的C ++非标准变体,并且不提供任何功能。在纯标准C ++中为其生成GUI程序的方法。 It's not impossible to use only standard C++ for .NET, and indeed one main reason to use C++ with .NET is to combine standard C++ parts with more .NET-y parts; 在.NET中仅使用标准C ++并非不可能 ,实际上,将C ++与.NET结合使用的主要原因之一是将标准C ++部件与更多.NET-y部件结合在一起。 it's just that using only standard C++ the code becomes complex, inefficient and generally impractical. 仅使用标准C ++会使代码变得复杂,低效并且通常不切实际。 For example, my first questions here on SO concerned how to do a simple .NET message box in standard C++ by way of COM technology as an intermediate layer, and that worked, but with a ridiculous amount of complex non-general support code. 例如,我在SO上的第一个问题是关于如何通过COM技术作为中间层在标准C ++中创建一个简单的.NET消息框,该消息框有效,但具有大量的复杂的非通用支持代码。

As another example, using C++ in web pages is very much an impedance mismatch, so to speak. 另一个例子是,在网页中使用C ++可以说是阻抗不匹配。 Again Microsoft is the main player who has attempted this, with their ActiveX technology. 微软再次凭借其ActiveX技术成为尝试此操作的主要参与者。 It turned out to be a good way ensure that PCs all over the world got infested with malware, because C++ is not the kind of language where it's easy to constrain what the code can do. 事实证明,这是确保全世界的PC都被恶意软件感染的好方法,因为C ++并不是一种很容易限制代码功能的语言。 As far as I know only Microsoft's own Internet Explorer browser ever supported ActiveX in web pages. 据我所知,只有Microsoft自己的Internet Explorer浏览器曾经在网页中支持ActiveX。 And as of Windows 10 the IE browser is being phased out and replaced by the newer Edge browser, that does not support ActiveX in web pages. 从Windows 10开始,IE浏览器已被淘汰,并由较新的Edge浏览器取代,该浏览器不支持网页中的ActiveX。

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

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