views:

116

answers:

4

I have a question about writing programs to be portable between windows and linux.

Recently I have realized that if you write a program that uses any sort of external library, if that library doesn't have a linux version (or a windows version when developing in linux) then you're screwed.

Here then is my question: if I write a program in linux that links to lol.a and then I want to compile and run it on windows without recompiling lol.a into lol.lib, can something like MinGW or Cygwin do this? Link to .a files on a Windows platform to result in an .exe file that can Windows can run?

+3  A: 

No, Cygwin provides (partial) source portability for *ix programs. Of course, there are higher level toolkits that also provide source portability, like QT and GTK. Either way, you still have to recompile the program and library. For binary portability, you'd need essentially the opposite of wine, a program that understood ELF and mapped Linux system and library calls to Windows ones. As far as I know, that doesn't exist.

Matthew Flaschen
Unfortunate, I would much prefer to go Linux->Windows than the other way around. With the exception of C++ of course, which has awful Linux support.
Matt Joiner
@Matt, "the exception of C++ of course, which has awful Linux support." Care to back that up?
Matthew Flaschen
+4  A: 

you will have to recompile all libraries for different operating systems. The binary formats for libraries vary from operating system to operating system. More importantly, even if you aren't using libraries, you need to recompile for the simple reason that the different operating systems have different syscall conventions. The only way to get around this is with a virtualizer.

In particular, CygWin cannot run linux programs. at all. CygWin provides only a posix compatibility layer between your program and the Windows kernel.

The situation is a bit less bleak on linux. Wine can run some native windows code (without the need to recompile anything, including your original code). But Wine also has limitations. It is not a complete Windows API, and anything that the library code requires to run must be available on Wine, or else it won't work either. For many, simple apps, this isn't a major problem, but many newer Windows API's, some dark corners of older ones that don't see much use, and in particular, anything that is hardware specific, probably won't be available.

If you intend to run on multiple platforms, it is urgent that you first verify that the libraries you intend to use are also cross platform, or that there are reasonable equivalents for all of the operating systems you wish to use.

TokenMacGuy
+1  A: 

No. You have to build it separately for Windows or Linux.

Scott Stafford
A: 

There are couple of suggestions given your situation:

  1. Develop the entire code for Windows, compile and run it.
  2. On Linux, use the WINE emulator http://www.winehq.org/download/
  3. If you choose to develop the code in Linux, look into Windows SFU http://en.wikipedia.org/wiki/Microsoft_Windows_Services_for_UNIX
  4. If possible post us what kind of s/w you are trying to develop -- 3rd party libraries like boost [http://www.boost.org] have a whole host of functionality that is platform independent. You definitely want to check out the options that boost gives you. Also check out other open source sites like github etc.
  5. Don't get into the habit of using platform specific libraries. I know they make life easier at times, but that is more than compensated when you need the code on a different platform.
Fanatic23
"Wine Is Not an Emulator" http://www.winehq.org/myths
Ben Voigt
Or... just use Qt.
LiraNuna