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

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


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.


C++ is cross platform. The problem you seem to have is that you are using platform dependent libraries.

I assume you are really talking about UI componenets- in which case I suggest using something like GTK+, Qt, or wxWindows- each of which have UI components that can be compiled for different systems.

The only solution is for you to find and use platform independent libraries.

And, on a side note, neither cygwin or Wine are emulation- they are 100% native implementations of the same functionality found their respective systems.


Once you're aware of the gotchas, it's actually not that hard. All of the code I am currently working on compiles on 32 and 64-bit Windows, all flavors of Linux, as well as Unix (Sun, HP and IBM). Obviously, these are not GUI products. Also, we don't use third-party libraries, unless we're compiling them ourselves.

I have one .h file that contains all of the compiler-specific code. For example, Microsoft and gcc disagree on how to specify an 8-bit integer. So in the .h, I have

#if defined(_MSC_VER)   typedef __int8 int8_t; #elif defined(__unix)   typedef char int8_t; #endif

There's also quite a bit of code that uniformizes certain lower-level function calls, for example:

#if defined(_MSC_VER)  #define SplitPath(Path__,Drive__,Name__,Ext__) _splitpath(Path__,Drive__,Dir__,Name__,Ext__)#elif defined(__unix)  #define SplitPath(Path__,Drive__,Name__,Ext__) UnixSplitPath(Path__,Drive__,Name__,Ext__)#endif

Now in this case, I believe I had to write a UnixSplitPath() function - there will be times when you need to. But most of the time, you just have to find the correct replacement function. In my code, I'll call SplitPath(), even though it's not a native function on either platform; the #defines will sort it out for me. It takes a while to train yourself.

Believe it or not, my .h file is only 240 lines long. There's really not much to it. And that includes handling endian issues.

Some of the lower-level stuff will need conditional compilation. For example, in Windows, I use Critical Sections, but in Linux I need to use pthread_mutex's. CriticalSection's were encapsulated in a class, and this class has a good deal of conditional compilation. However, the upper-level program is totally unaware, the class functions exactly the same regardless of the platform.

The other secret I can give you is: build your project on all platforms often (particularly at the beginning). It is a lot easier when you nip the compiler problems in the bud. Don't wait until you're done development before you attempt to go cross-platform.