How do I write a C++ program that will easily compile in Linux and Windows?

Unknown picture Unknown · Jan 25, 2009 · Viewed 7.1k times · Source

I am making a C++ program.

One of my biggest annoyances with C++ is its supposed platform independence.

You all probably know that it is pretty much impossible to compile a Linux C++ program in Windows and a Windows one to Linux without a deluge of cryptic errors and platform specific include files.

Of course you can always switch to some emulation like Cygwin and wine, but I ask you, is there really no other way?

Answer

user54808 picture user54808 · Jan 25, 2009

The language itself is cross-platform but most libraries are not, but there are three things that you should keep in mind if you want to go completely cross-platform when programming in C++.

Firstly, you need to start using some kind of cross-platform build system, like SCons. Secondly, you need to make sure that all of the libraries that you are using are built to be cross-platform. And a minor third point, I would recommend using a compiler that exists on all of your target platforms, gcc comes in mind here (C++ is a rather complex beast and all compilers have their own specific quirks).

I have some further suggestions regarding graphical user interfaces for you. There are several of these available to use, the three most notable are:

GTK+ and QT are two API's that come with their own widget sets (buttons, lists, etc.), whilst wxWidgets is more of a wrapper API to the currently running platforms native widget set. This means that the two former might look a bit differently compared to the rest of the system whilst the latter one will look just like a native program.

And if you're into games programming there are equally many API's to choose from, all of them cross-platform as well. The two most fully featured that I know of are:

Both of which contains everything from graphics to input and audio routines, either through plugins or built-in.

Also, if you feel that the standard library in C++ is a bit lacking, check out Boost for some general purpose cross-platform sweetness.

Good Luck.