Vordreller
Vordreller

Reputation: 2530

Why are some programs written in C++ windows-only and others are not?

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.

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

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?

Upvotes: 1

Views: 2431

Answers (8)

OISnotloggedin
OISnotloggedin

Reputation:

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

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

See PHP coding standards.

Upvotes: 0

Pete Kirkham
Pete Kirkham

Reputation: 49311

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

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.

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

Upvotes: 1

Larry Gritz
Larry Gritz

Reputation: 13690

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

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.

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.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346300

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.

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.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104178

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.

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.

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.

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.

Upvotes: 11

Steve Rowe
Steve Rowe

Reputation: 19413

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.

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.

Upvotes: 3

tunnuz
tunnuz

Reputation: 23978

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).
  • 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, ...).

Upvotes: 4

womble
womble

Reputation: 12407

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.

Upvotes: 1

Related Questions