C++ Windows 和 Linux 之间的可移植性
我有一个关于编写可在 Windows 和 Linux 之间移植的程序的问题。
最近我意识到,如果你编写一个使用任何类型的外部库的程序,如果该库没有 Linux 版本(或者在 Linux 中开发时没有 Windows 版本),那么你就完蛋了。
那么我的问题是:如果我在linux中编写一个链接到lol.a的程序,然后我想在windows上编译并运行它而不将lol.a重新编译成lol.lib,像MinGW或Cygwin这样的东西可以做到这一点吗?链接到Windows平台上的.a文件以生成Windows可以运行的.exe文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将必须为不同的操作系统重新编译所有库。库的二进制格式因操作系统而异。更重要的是,即使您不使用库,您也需要重新编译,原因很简单,不同的操作系统有不同的系统调用约定。解决这个问题的唯一方法是使用虚拟器。
特别是,CygWin 无法运行 Linux 程序。根本不。 CygWin 仅在您的程序和 Windows 内核之间提供 posix 兼容层。
Linux 上的情况没那么暗淡。 Wine 可以运行一些本机 Windows 代码(无需重新编译任何内容,包括您的原始代码)。但 Wine 也有局限性。它不是一个完整的 Windows API,库代码运行所需的任何内容都必须在 Wine 上可用,否则它也将无法工作。对于许多简单的应用程序来说,这不是一个主要问题,但许多较新的 Windows API、旧 API 的一些阴暗角落没有太多用处,特别是任何特定于硬件的内容可能不可用。
如果您打算在多个平台上运行,那么迫切需要首先验证您打算使用的库是否也是跨平台的,或者对于您希望使用的所有操作系统都有合理的等效项。
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.
不,Cygwin 为 *ix 程序提供(部分)源代码可移植性。当然,还有更高级别的工具包也提供源代码可移植性,例如 QT 和 GTK。不管怎样,你仍然需要重新编译程序和库。对于二进制可移植性,您基本上需要与 wine 相反的程序,它是一个能够理解 ELF 并将 Linux 系统和库调用映射到 Windows 的程序。据我所知,这是不存在的。
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.
不可以。您必须为 Windows 或 Linux 单独构建它。
No. You have to build it separately for Windows or Linux.
根据您的情况,有一些建议:
There are couple of suggestions given your situation: