views:

848

answers:

8

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?

+1  A: 

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.

womble
+4  A: 

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

tunnuz
+11  A: 

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.

kgiannakakis
I agree with all except the last sentence. If the developer had chosen a platform-agnostic/cross-platform GUI toolkit, it would have been rather simple to make the application cross-platform. My guess is the developer is comfortable with the libraries used.
Raithlin
Many open source applications start as hobby projects of a single developer. Usually they target one single platform. Afterwards other developers jump in, with expertise in other architectures and make it cross platform. It is difficult (not impossible) for a single person to do everything.
kgiannakakis
It depends on what toolkit you are using, but sometimes/often you just need to recompile. It's really not that difficult for single person to write cross platform application, especially if you start from scratch. What is sometimes difficult is to change existing application to be cross platform
Luka Marinko
For a serious project you need to test, maintain and support all ports. Not only you need a new environment set up, but every time you make a code change you need to rerun unit tests and do system testing. You should also monitor bugs and answer user's questions. That's a lot of work.
kgiannakakis
+3  A: 

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.

Steve Rowe
Neither the Java nor the C# language offer more functionality than C++ in this regard. All of the functionality is provided by the underlying framework. I also disagree with your statement that writing cross platform C++ programs "isn't often done".
Dave Van den Eynde
The "underlying frameworks" are required to be there for a conforming implementation of Java/C# though, as far as I know. And there's no doubt that the *vast* majority of C++ code is not cross-platform.
jalf
I disagree. Variations like the .NET Micro Framework and the customizable Mono runtime prove that you don't need a massive underlying framework to work with C#. I think Java has similar options.
Dave Van den Eynde
+1  A: 

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.

Pete Kirkham
Hah! I like the first answer. :)
wuputah
A: 

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.

A: 

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.

Michael Borgwardt
A: 

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.

Larry Gritz