Cross-platform C++

Started by
11 comments, last by Codeka 15 years, 1 month ago
Is it possible to create an executable which meets these requirements? 1. Will run the same on both Windows and Linux machines, and perhaps Mac too 2. Contains advanced graphics (OpenGL) 3. Can be compiled directly in Windows If so, any pointers to relevant compilers, tutorials, etc. would be very welcome. I have tried searching the internet for these myself, but wasn't sure of the results. Thanks for your time. [smile]
Advertisement
You can't run the same binary on Windows, Linux and Mac. You can compile the same source code and produce a different binary for each platform however.

You you really want the same binary to run on Windows, Linux and Mac then you're looking at something like Java or .NET/mono.
Quote:Original post by Codeka
You can't run the same binary on Windows, Linux and Mac. You can compile the same source code and produce a different binary for each platform however.

You you really want the same binary to run on Windows, Linux and Mac then you're looking at something like Java or .NET/mono.


Actually you can since both Linux and Mac can run Windows software as long as you're running on x86 hardware (Using compatibility layers such as Wine), As long as he only uses the standard Win32 API, avoids anything that requires custom drivers (alot of DRM solutions do this) and sticks to OpenGL or a reasonably old DX version (DX8 is pretty well supported and the more commonly used parts of DX9 works well enough) he should be able to get things to run pretty well on Linux and Mac with very little additional effort.

Its worth noting that the Linux version of Firefox actually runs slower than the Windows version does in Wine on the same OS, so performance using Wine can be as good or even better than a native application. Source: http://www.tuxradar.com/content/browser-benchmarks-2-even-wine-beats-linux-firefox

Wine is a very mature project, Its not 100% compatible with all Windows APIs (yet atleast), but it is pretty darn close when it comes to Win32 and older DX versions and as long as you still test properly on the other platforms it is perfectly viable for cross platform development. (There are better options, but if you allready have alot of Windows code it might be faster/cheaper to adjust that to work with Wine than to rewrite everything using other APIs)

Ofcourse if you truly consider Linux a target you might want to consider supporting non x86 platforms as well which pretty much requires the use of a good cross platform library (There are some ARM based netbooks on their way with some pretty impressive battery times (AI claims that their "Touch Book" has a battery time of 10-15 hours which would make it a rather interesting product despite the fact that it is unable to run Windows or Windows software (Unless you count Windows CE))
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by SimonForsman
Quote:Original post by Codeka
You can't run the same binary on Windows, Linux and Mac. You can compile the same source code and produce a different binary for each platform however.

You you really want the same binary to run on Windows, Linux and Mac then you're looking at something like Java or .NET/mono.


Actually you can since both Linux and Mac can run Windows software as long as you're running on x86 hardware (Using compatibility layers such as Wine), As long as he only uses the standard Win32 API, avoids anything that requires custom drivers (alot of DRM solutions do this) and sticks to OpenGL or a reasonably old DX version (DX8 is pretty well supported and the more commonly used parts of DX9 works well enough) he should be able to get things to run pretty well on Linux and Mac with very little additional effort.

Its worth noting that the Linux version of Firefox actually runs slower than the Windows version does in Wine on the same OS, so performance using Wine can be as good or even better than a native application. Source: http://www.tuxradar.com/content/browser-benchmarks-2-even-wine-beats-linux-firefox

Wine is a very mature project, Its not 100% compatible with all Windows APIs (yet atleast), but it is pretty darn close when it comes to Win32 and older DX versions and as long as you still test properly on the other platforms it is perfectly viable for cross platform development. (There are better options, but if you allready have alot of Windows code it might be faster/cheaper to adjust that to work with Wine than to rewrite everything using other APIs)

A lot really depends on the Linux distro used but yeah I was quite suprised a while back when I was testing out the latest Suse distro I believe and double clicked an executable I built in windows and it actually ran and couldn't figure out how it happened until I realized Suse was actually usen Wine to do it!


[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Windows runs only on x86 architecture and uses the COFF PEI binary format.

Mac OS X runs on PowerPC, x86, and ARM (iPhone) architectures and uses the MACH-O binary format and usually in the form of application bundles.

Linux runs on everything in creation and then some, and uses the ELF binary
format.

All the target platforms also use different packaging, installation, and distribution mechanisms, and that's very important to consider. Windows tends to used self-extracting installer applications that users download from third-party sources. Mac OS X often uses application bundles that can be gleaned from third-party sources and just dropped in the Application folder. There is no "Linux" OS but rather many different ones, and the most common distribution mech uses either RPM files or DEB files downloaded and installed from a distribution or a third-party repository and installed using a native package manager.

The binary is only one small part of a consumer-level software package.

If you are running Mac OS X or Linux on x86 you can obtain a simulator system called 'wine' that provides much of the Microsoft Windows OS as cleanroom-developed libraries, emulation layers, and a COFF interpreter. Support is not always complete but over the years there has been consistent effort to catch up with Microsoft.

Keep in mind that the future of mass computing is not necessarily x86. The consumer computer industry is no longer driven by American firms outsourcing manufacturing to Taiwan but by Taiwanese firms outsourcing marketing to American firms. The Taiwanese prefer ARM with its lower per-unit cost, power consumption, physical footprint, and it's wider supply chain. The next generation of netbooks will be ARM-based, and it's going to be hard for Microsoft to ramp up the ecosystem it will need to sell an ARM-based Windows product effectively. Netbook v.2 is going to be all-Linux, at least for a while, and in particular Red Flag, Ubuntu, and Xandros.

So, in short, it's possible to create a binary and have it run on Windows, Mac OS X, and some Linuxes, but you would be better off if you targetted a multi-platform compile (ie. build native apps on each target platform) instead.

Stephen M. Webb
Professional Free Software Developer

java?
Thanks very much for your responses. [smile]

Quote:Original post by Codeka
You can't run the same binary on Windows, Linux and Mac. You can compile the same source code and produce a different binary for each platform however.


How would I write source code which compiles on both Windows and Linux? Would I have to avoid 'windows.h', or use something like this:

#ifdef WIN32#include windows.h[message loop, etc]#endif#ifdef LINUX#include linux.h[message loop, etc]#endif

Quote:Original post by Sappharos
How would I write source code which compiles on both Windows and Linux? Would I have to avoid 'windows.h', or use something like this:

*** Source Snippet Removed ***

There are several ways to do it, and which one is better will depend on your specific needs. Boost has a good overview of different strategies to handle platform differences here.
As long as you use all cross-platform libraries, I don't see a huge problem

Say you're using SDL for input, sound (could be OpenAL), windowing
ENet for networking
OpenGl for the 3D graphics

There are some small changes from platform to platform (you need SDLMain on Mac, the headers are a little different, yada) but they're not enough to cripple a project or anything. Just stay far, far away from the win32 api.

I would seriously not recommend relying on WINE to handle the work for you. You could do it, I just think it would be easier to use cross-platform libraries that have done this work for you.

Then again, I could be totally wrong. Here's hoping I'm not.
Well in many situations you are forced to use specific platform specific libraries even if you were to use cross platform libraries. In such scenarios you write small top layer wrapper which hides the specific implementations for specific platforms and thus giving you a clean and single interface for all platforms.
The more applications I write, more I find out how less I know

This topic is closed to new replies.

Advertisement