C++ Portability between Windows and Linux

為{幸葍}努か 提交于 2019-12-01 21:49:40

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.


来源:https://stackoverflow.com/questions/3261608/c-portability-between-windows-and-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!