为什么有些程序是用C ++编写的,而其他程序则不是?

时间:2022-10-31 05:52:20

That's something I've been wondering for a while now.

这是我一直想知道的事情。

Take Notepad++ for instace. Wikipedia tells me it was written in C++ and it's Windows-only.

以记事本++为例。*告诉我它是用C ++编写的,它只是Windows版本。

Now take PHP. Wikipedia tells me this is also written in C++, but that runs on other OS too.

现在拿PHP。*告诉我这也是用C ++编写的,但也可以在其他操作系统上运行。

But I see more languages then just C++ for PHP... how is this done? Do they make some new code in C++, see it works and then figure out how to do it in Perl, or what happens?

但我看到更多的语言,然后只是C ++ for PHP ...这是怎么做到的?他们是否在C ++中创建了一些新代码,看到它有效,然后弄清楚如何在Perl中执行它,或者会发生什么?

8 个解决方案

#1


It depends whether you are using platform specific libraries or not. Notepad++ is a desktop application and it needs a GUI toolkit. Although there are cross-platform C++ libraries like Qt and wxWidgets, Notepad++ is probably using a Microsoft's specific technology. Thus it can't be ported in other platforms.

这取决于您是否使用特定于平台的库。 Notepad ++是一个桌面应用程序,它需要一个GUI工具包。虽然有像Qt和wxWidgets这样的跨平台C ++库,但Notepad ++可能正在使用微软的特定技术。因此它无法移植到其他平台中。

PHP on the other side is a WEB scripting technology so there's no need of GUI library. Also there is much more stronger interest in running PHP in many platforms than there is for Notepad++. That is an incentive for the developers to make the C++ code cross platform.

另一方面,PHP是一种WEB脚本技术,因此不需要GUI库。与Notepad ++相比,在许多平台上运行PHP的兴趣要大得多。这是开发人员使C ++代码跨平台的动机。

Avoid platform specific libraries isn't the only thing needed for a C++ cross platform application. It usually means coding for the least common denominator and keeping different code branches for every platform supported. Although C++ is a cross platform language, each system has its own intricacies. In fact the code could be different in the same platform as well, if a different compiler was to be used. Try downloading the C++ source of an open source application, like PHP for example. You would notice that much of the code is the same for all platforms, but there would be different bits also. Sometimes preprocessor directives are used, elsewhere totally different source files are involved.

避免使用特定于平台的库不是C ++跨平台应用程序所需的唯一内容。它通常意味着编码最小公分母并为每个支持的平台保留不同的代码分支。虽然C ++是一种跨平台语言,但每个系统都有自己的复杂性。事实上,如果要使用不同的编译器,代码在同一平台上也可能不同。尝试下载开源应用程序的C ++源代码,例如PHP。您会注意到所有平台的大部分代码都是相同的,但也会有不同的位。有时使用预处理程序指令,其他地方涉及完全不同的源文件。

So creating a true cross-platform C++ application is a hard job and it is usually created when there is a strong incentive to do so and many people are involved. An one-man application like Notepad++ really can't be cross-platform.

因此,创建一个真正的跨平台C ++应用程序是一项艰巨的工作,它通常是在有强烈动机的情况下创建的,并且很多人都参与其中。像Notepad ++这样的单人应用程序实际上不能跨平台。

#2


Usually programs that work on a single platform make use of some facilities from the operating system (e.g. system calls to handle windows, buttons, services). Obviously the code is strictly system-dependent and cannot work in other environments. For cross-platform software you can follow several approaches, some of them are:

通常,在单个平台上工作的程序使用来自操作系统的一些工具(例如,系统调用来处理窗口,按钮,服务)。显然,代码严格依赖于系统,不能在其他环境中工作。对于跨平台软件,您可以遵循几种方法,其中一些方法是:

  • You program independently from the operating system and the stuff that lays under your feet (so you don't use operating system facilities, frameworks and so on).
  • 您可以独立于操作系统和脚下的东西进行编程(因此您不使用操作系统工具,框架等)。

  • You program using libraries and tools that are programmed that way, and thus work under several operating systems (for instance you use Qt or wxWindows, that are cross-platform, to manage windows and interfaces).
  • 您使用以这种方式编程的库和工具进行编程,因此可以在多个操作系统下工作(例如,您使用跨平台的Qt或wxWindows来管理窗口和接口)。

  • You build several versions of the code, to handle the particularities of the various architectures it will run on. This way you probably achieve more performance and "better" software because the code is optimized for its own architecture, but can be very difficult to maintain.
  • 您构建了几个版本的代码,以处理它将运行的各种体系结构的特殊性。这样您就可以获得更高的性能和“更好”的软件,因为代码针对自己的架构进行了优化,但是很难维护。

I think that most languages work in the first way, but sometimes they could use operating system calls to gain some performance (in this case you will have e.g. PHP for Windows, PHP for MacOS, ...).

我认为大多数语言都是以第一种方式工作,但有时他们可以使用操作系统调用来获得一些性能(在这种情况下,您将拥有例如PHP for Windows,PHP for MacOS,...)。

#3


Unlike some modern languages like Java and C#, C++ the language provides only basic functionality. It has no standard way of handling the user interface, threads, network interaction, cryptography, or even reading XML. Instead, support for this sort of functionality is left up to the operating system. Windows provides a broad API set called Win32 that applications written for Windows take advantage. There are similar APIs built on top of Linux and other operating systems. Whenever authors take advantage of the libraries of a specific operating system, they make it so their programs won't work on other operating systems.

与Java和C#,C ++等现代语言不同,该语言仅提供基本功能。它没有标准的方法来处理用户界面,线程,网络交互,加密,甚至读取XML。相反,对这种功能的支持由操作系统决定。 Windows提供了一个名为Win32的广泛API集,为Windows编写的应用程序可以利用它们。在Linux和其他操作系统之上构建了类似的API。每当作者利用特定操作系统的库时,他们就会使其程序无法在其他操作系统上运行。

It is possible to write a C++ program that will work across operating systems by abstracting away the items that interact with the operating system, but this is not simple and isn't often done.

通过抽象出与操作系统交互的项目,可以编写一个可以跨操作系统工作的C ++程序,但这并不简单,也不常用。

#4


C++ is an inherently cross-platform language; it's just that there are lots of libraries written in C++ that use features specific to a particular platform, so those programs are limited to that platform. Avoid those platform-specific features, and you've got yourself a C++ program that should compile on most platforms without too much hassle.

C ++本质上是跨平台的语言;只是有很多用C ++编写的库使用特定于特定平台的特性,因此这些程序仅限于该平台。避免那些特定于平台的功能,并且你已经拥有了一个C ++程序,它应该在大多数平台上编译而不会有太多麻烦。

#5


Notepad++ is Windows only because Notepad++ is a replacement for Windows Notepad, which is notoriously bad. No-one on any self-respecting OS would use an editor with 'Notepad' in the name ;>.

Notepad ++只是Windows,因为Notepad ++是Windows Notepad的替代品,这是非常糟糕的。任何自尊操作系统上的任何人都不会使用名称中包含“记事本”的编辑器;>。

The core of Notepad++ is the Scintilla text editor component, which is cross-platform (Linux, OS X and Windows) and is used in both cross-platform and platform-specific applications. The core view logic is shared, with abstract classes for interfacing with windowing systems and graphics contexts. The applications take the cross-platform component and bind it to a OS specific window and graphics context. My favourite editor on Linux and Windows is SciTE, which is a very fast light wrapper around the editor with Lua scripting; there's also Komodo which wraps the Scintilla editor component in a Mozilla XUL container - another cross platform C++ stack.

Notepad ++的核心是Scintilla文本编辑器组件,它是跨平台的(Linux,OS X和Windows),可用于跨平台和特定于平台的应用程序。核心视图逻辑是共享的,具有用于与窗口系统和图形上下文连接的抽象类。应用程序采用跨平台组件并将其绑定到特定于操作系统的窗口和图形上下文。我最喜欢的Linux和Windows编辑器是SciTE,它是一个围绕编辑器的快速包装器,带有Lua脚本;还有Komodo将Scintilla编辑器组件包装在Mozilla XUL容器中 - 另一个跨平台C ++堆栈。

There is some cost to making applications cross-platform; it's only worth that investment if there are prospective users on those platforms.

使应用程序跨平台需要一些成本;如果在这些平台上有潜在用户,那么投资是值得的。

#6


Now take PHP. Wikipedia tells me this is also written in C++, but that runs on other OS too.

现在拿PHP。*告诉我这也是用C ++编写的,但也可以在其他操作系统上运行。

PHP is written in C only to make it available on more platforms. There are even rules to only make C style comments, not C++ //.

PHP是用C语言编写的,只是为了让它在更多平台上可用。甚至有规则只能制作C风格的评论,而不是C ++ //。

See PHP coding standards.

请参阅PHP编码标准。

#7


Apart from platform-specific libraries and APIs, C and C++ are also not truely cross-platfrom languages (for a good reason). They intentionally leave a lot of details unspecified, like type lengths, endianness, variable alignment in structs and whether newly allocated memory is initialzed.

除了特定于平台的库和API之外,C和C ++也不是真正的跨语言语言(出于好的理由)。他们故意留下许多未指定的细节,例如类型长度,字节序,结构中的变量对齐以及是否初始化新​​分配的内存。

This allows you to write (platform-specific) code that is as fast as your CPU can possibly go, but it also means that if you want your code to be portable, it takes some additional effort and testing - which many people targeting the Windows/x86 platform probably skip.

这允许您编写(特定于平台的)代码,其速度与CPU可能的速度一样快,但这也意味着如果您希望代码可移植,则需要额外的工作和测试 - 许多人都针对Windows / x86平台可能会跳过。

#8


Others have commented on GUIs and use of other libraries that exist on only a subset of major platforms.

其他人评论了GUI以及仅存在于主要平台子集上的其他库的使用。

Another factor is the developers. Many developers (or software companies) only have expertise, access to, customer demand for, or need to use a single platform, and so they don't spend the extra effort to make their software cross-platform. For example, if a company has a Windows PC and developer tools on everybody's desk, no Linux or Mac machines in house to develop or test on, and no developers who are experts on those other platforms, and no large customers demanding a different platform, it's hard for them to justify not just plowing ahead with a Windows-only package. And if they change their mind later, when they have the expertise, equipment, or additional requirements, they may find it's too late to fix a large code base that has been allowed to become very platform-dependent.

另一个因素是开发人员。许多开发人员(或软件公司)只有专业知识,访问权限,客户需求或需要使用单一平台,因此他们不会花费额外的精力来使他们的软件跨平台。例如,如果一家公司在每个人的桌面上都有Windows PC和开发人员工具,那么没有内部开发或测试的Linux或Mac机器,也没有那些在其他平台上是专家的开发人员,也没有大客户要求不同的平台,他们很难证明不仅仅是使用仅限Windows的软件包。如果他们以后改变主意,当他们拥有专业知识,设备或其他要求时,他们可能会发现修复一个已经被允许变得非常依赖于平台的大型代码库已经太晚了。

Platform-independence takes real effort, every step of the way. If you start from day one with a plan, use cross-platform libraries (e.g., Qt, boost, OpenGL, etc., carefully avoiding MFC, DirectX, etc.), and build and test on all platforms regularly, it's probably only 10-20% more effort to make a good cross-platform app. But if you start with a one-platform app that's been in development for a long time, making it cross-platform can take as much effort as writing it from scratch, and that can be especially hard to justify if the new platform has a comparatively small market share in your industry or if your developers hate working on it.

平*立需要付出实际的努力,每一步都是如此。如果你从计划的第一天开始,使用跨平台库(例如,Qt,boost,OpenGL等,小心避免使用MFC,DirectX等),并定期在所有平台上构建和测试,它可能只有10个制作一个优秀的跨平台应用程序需要多花20%的精力。但是如果你开始使用已经开发了很长时间的单平台应用程序,那么跨平台创建它可以花费很多努力从而从头开始编写,如果新平台具有相对较高的特性,那么这一点尤其难以证明您所在行业的小市场份额,或者您的开发人员不喜欢它。

#1


It depends whether you are using platform specific libraries or not. Notepad++ is a desktop application and it needs a GUI toolkit. Although there are cross-platform C++ libraries like Qt and wxWidgets, Notepad++ is probably using a Microsoft's specific technology. Thus it can't be ported in other platforms.

这取决于您是否使用特定于平台的库。 Notepad ++是一个桌面应用程序,它需要一个GUI工具包。虽然有像Qt和wxWidgets这样的跨平台C ++库,但Notepad ++可能正在使用微软的特定技术。因此它无法移植到其他平台中。

PHP on the other side is a WEB scripting technology so there's no need of GUI library. Also there is much more stronger interest in running PHP in many platforms than there is for Notepad++. That is an incentive for the developers to make the C++ code cross platform.

另一方面,PHP是一种WEB脚本技术,因此不需要GUI库。与Notepad ++相比,在许多平台上运行PHP的兴趣要大得多。这是开发人员使C ++代码跨平台的动机。

Avoid platform specific libraries isn't the only thing needed for a C++ cross platform application. It usually means coding for the least common denominator and keeping different code branches for every platform supported. Although C++ is a cross platform language, each system has its own intricacies. In fact the code could be different in the same platform as well, if a different compiler was to be used. Try downloading the C++ source of an open source application, like PHP for example. You would notice that much of the code is the same for all platforms, but there would be different bits also. Sometimes preprocessor directives are used, elsewhere totally different source files are involved.

避免使用特定于平台的库不是C ++跨平台应用程序所需的唯一内容。它通常意味着编码最小公分母并为每个支持的平台保留不同的代码分支。虽然C ++是一种跨平台语言,但每个系统都有自己的复杂性。事实上,如果要使用不同的编译器,代码在同一平台上也可能不同。尝试下载开源应用程序的C ++源代码,例如PHP。您会注意到所有平台的大部分代码都是相同的,但也会有不同的位。有时使用预处理程序指令,其他地方涉及完全不同的源文件。

So creating a true cross-platform C++ application is a hard job and it is usually created when there is a strong incentive to do so and many people are involved. An one-man application like Notepad++ really can't be cross-platform.

因此,创建一个真正的跨平台C ++应用程序是一项艰巨的工作,它通常是在有强烈动机的情况下创建的,并且很多人都参与其中。像Notepad ++这样的单人应用程序实际上不能跨平台。

#2


Usually programs that work on a single platform make use of some facilities from the operating system (e.g. system calls to handle windows, buttons, services). Obviously the code is strictly system-dependent and cannot work in other environments. For cross-platform software you can follow several approaches, some of them are:

通常,在单个平台上工作的程序使用来自操作系统的一些工具(例如,系统调用来处理窗口,按钮,服务)。显然,代码严格依赖于系统,不能在其他环境中工作。对于跨平台软件,您可以遵循几种方法,其中一些方法是:

  • You program independently from the operating system and the stuff that lays under your feet (so you don't use operating system facilities, frameworks and so on).
  • 您可以独立于操作系统和脚下的东西进行编程(因此您不使用操作系统工具,框架等)。

  • You program using libraries and tools that are programmed that way, and thus work under several operating systems (for instance you use Qt or wxWindows, that are cross-platform, to manage windows and interfaces).
  • 您使用以这种方式编程的库和工具进行编程,因此可以在多个操作系统下工作(例如,您使用跨平台的Qt或wxWindows来管理窗口和接口)。

  • You build several versions of the code, to handle the particularities of the various architectures it will run on. This way you probably achieve more performance and "better" software because the code is optimized for its own architecture, but can be very difficult to maintain.
  • 您构建了几个版本的代码,以处理它将运行的各种体系结构的特殊性。这样您就可以获得更高的性能和“更好”的软件,因为代码针对自己的架构进行了优化,但是很难维护。

I think that most languages work in the first way, but sometimes they could use operating system calls to gain some performance (in this case you will have e.g. PHP for Windows, PHP for MacOS, ...).

我认为大多数语言都是以第一种方式工作,但有时他们可以使用操作系统调用来获得一些性能(在这种情况下,您将拥有例如PHP for Windows,PHP for MacOS,...)。

#3


Unlike some modern languages like Java and C#, C++ the language provides only basic functionality. It has no standard way of handling the user interface, threads, network interaction, cryptography, or even reading XML. Instead, support for this sort of functionality is left up to the operating system. Windows provides a broad API set called Win32 that applications written for Windows take advantage. There are similar APIs built on top of Linux and other operating systems. Whenever authors take advantage of the libraries of a specific operating system, they make it so their programs won't work on other operating systems.

与Java和C#,C ++等现代语言不同,该语言仅提供基本功能。它没有标准的方法来处理用户界面,线程,网络交互,加密,甚至读取XML。相反,对这种功能的支持由操作系统决定。 Windows提供了一个名为Win32的广泛API集,为Windows编写的应用程序可以利用它们。在Linux和其他操作系统之上构建了类似的API。每当作者利用特定操作系统的库时,他们就会使其程序无法在其他操作系统上运行。

It is possible to write a C++ program that will work across operating systems by abstracting away the items that interact with the operating system, but this is not simple and isn't often done.

通过抽象出与操作系统交互的项目,可以编写一个可以跨操作系统工作的C ++程序,但这并不简单,也不常用。

#4


C++ is an inherently cross-platform language; it's just that there are lots of libraries written in C++ that use features specific to a particular platform, so those programs are limited to that platform. Avoid those platform-specific features, and you've got yourself a C++ program that should compile on most platforms without too much hassle.

C ++本质上是跨平台的语言;只是有很多用C ++编写的库使用特定于特定平台的特性,因此这些程序仅限于该平台。避免那些特定于平台的功能,并且你已经拥有了一个C ++程序,它应该在大多数平台上编译而不会有太多麻烦。

#5


Notepad++ is Windows only because Notepad++ is a replacement for Windows Notepad, which is notoriously bad. No-one on any self-respecting OS would use an editor with 'Notepad' in the name ;>.

Notepad ++只是Windows,因为Notepad ++是Windows Notepad的替代品,这是非常糟糕的。任何自尊操作系统上的任何人都不会使用名称中包含“记事本”的编辑器;>。

The core of Notepad++ is the Scintilla text editor component, which is cross-platform (Linux, OS X and Windows) and is used in both cross-platform and platform-specific applications. The core view logic is shared, with abstract classes for interfacing with windowing systems and graphics contexts. The applications take the cross-platform component and bind it to a OS specific window and graphics context. My favourite editor on Linux and Windows is SciTE, which is a very fast light wrapper around the editor with Lua scripting; there's also Komodo which wraps the Scintilla editor component in a Mozilla XUL container - another cross platform C++ stack.

Notepad ++的核心是Scintilla文本编辑器组件,它是跨平台的(Linux,OS X和Windows),可用于跨平台和特定于平台的应用程序。核心视图逻辑是共享的,具有用于与窗口系统和图形上下文连接的抽象类。应用程序采用跨平台组件并将其绑定到特定于操作系统的窗口和图形上下文。我最喜欢的Linux和Windows编辑器是SciTE,它是一个围绕编辑器的快速包装器,带有Lua脚本;还有Komodo将Scintilla编辑器组件包装在Mozilla XUL容器中 - 另一个跨平台C ++堆栈。

There is some cost to making applications cross-platform; it's only worth that investment if there are prospective users on those platforms.

使应用程序跨平台需要一些成本;如果在这些平台上有潜在用户,那么投资是值得的。

#6


Now take PHP. Wikipedia tells me this is also written in C++, but that runs on other OS too.

现在拿PHP。*告诉我这也是用C ++编写的,但也可以在其他操作系统上运行。

PHP is written in C only to make it available on more platforms. There are even rules to only make C style comments, not C++ //.

PHP是用C语言编写的,只是为了让它在更多平台上可用。甚至有规则只能制作C风格的评论,而不是C ++ //。

See PHP coding standards.

请参阅PHP编码标准。

#7


Apart from platform-specific libraries and APIs, C and C++ are also not truely cross-platfrom languages (for a good reason). They intentionally leave a lot of details unspecified, like type lengths, endianness, variable alignment in structs and whether newly allocated memory is initialzed.

除了特定于平台的库和API之外,C和C ++也不是真正的跨语言语言(出于好的理由)。他们故意留下许多未指定的细节,例如类型长度,字节序,结构中的变量对齐以及是否初始化新​​分配的内存。

This allows you to write (platform-specific) code that is as fast as your CPU can possibly go, but it also means that if you want your code to be portable, it takes some additional effort and testing - which many people targeting the Windows/x86 platform probably skip.

这允许您编写(特定于平台的)代码,其速度与CPU可能的速度一样快,但这也意味着如果您希望代码可移植,则需要额外的工作和测试 - 许多人都针对Windows / x86平台可能会跳过。

#8


Others have commented on GUIs and use of other libraries that exist on only a subset of major platforms.

其他人评论了GUI以及仅存在于主要平台子集上的其他库的使用。

Another factor is the developers. Many developers (or software companies) only have expertise, access to, customer demand for, or need to use a single platform, and so they don't spend the extra effort to make their software cross-platform. For example, if a company has a Windows PC and developer tools on everybody's desk, no Linux or Mac machines in house to develop or test on, and no developers who are experts on those other platforms, and no large customers demanding a different platform, it's hard for them to justify not just plowing ahead with a Windows-only package. And if they change their mind later, when they have the expertise, equipment, or additional requirements, they may find it's too late to fix a large code base that has been allowed to become very platform-dependent.

另一个因素是开发人员。许多开发人员(或软件公司)只有专业知识,访问权限,客户需求或需要使用单一平台,因此他们不会花费额外的精力来使他们的软件跨平台。例如,如果一家公司在每个人的桌面上都有Windows PC和开发人员工具,那么没有内部开发或测试的Linux或Mac机器,也没有那些在其他平台上是专家的开发人员,也没有大客户要求不同的平台,他们很难证明不仅仅是使用仅限Windows的软件包。如果他们以后改变主意,当他们拥有专业知识,设备或其他要求时,他们可能会发现修复一个已经被允许变得非常依赖于平台的大型代码库已经太晚了。

Platform-independence takes real effort, every step of the way. If you start from day one with a plan, use cross-platform libraries (e.g., Qt, boost, OpenGL, etc., carefully avoiding MFC, DirectX, etc.), and build and test on all platforms regularly, it's probably only 10-20% more effort to make a good cross-platform app. But if you start with a one-platform app that's been in development for a long time, making it cross-platform can take as much effort as writing it from scratch, and that can be especially hard to justify if the new platform has a comparatively small market share in your industry or if your developers hate working on it.

平*立需要付出实际的努力,每一步都是如此。如果你从计划的第一天开始,使用跨平台库(例如,Qt,boost,OpenGL等,小心避免使用MFC,DirectX等),并定期在所有平台上构建和测试,它可能只有10个制作一个优秀的跨平台应用程序需要多花20%的精力。但是如果你开始使用已经开发了很长时间的单平台应用程序,那么跨平台创建它可以花费很多努力从而从头开始编写,如果新平台具有相对较高的特性,那么这一点尤其难以证明您所在行业的小市场份额,或者您的开发人员不喜欢它。