C++ Windows 和 Linux 之间的可移植性

发布于 2024-09-09 19:47:25 字数 285 浏览 7 评论 0原文

我有一个关于编写可在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗弃M 2024-09-16 19:47:25

您将必须为不同的操作系统重新编译所有库。库的二进制格式因操作系统而异。更重要的是,即使您不使用库,您也需要重新编译,原因很简单,不同的操作系统有不同的系统调用约定。解决这个问题的唯一方法是使用虚拟器。

特别是,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.

夕色琉璃 2024-09-16 19:47:25

不,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.

花伊自在美 2024-09-16 19:47:25

不可以。您必须为 Windows 或 Linux 单独构建它。

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

我三岁 2024-09-16 19:47:25

根据您的情况,有一些建议:

  1. 开发适用于 Windows 的完整代码,编译并运行它。
  2. 在 Linux 上,使用 WINE 模拟器 http://www.winehq.org/download/
  3. 如果您选择在 Linux 中开发代码,请查看 Windows SFU http://en.wikipedia.org/wiki/ Microsoft_Windows_Services_for_UNIX
  4. 如果可能的话,请告诉我们您正在尝试开发哪种软件 - 3rd 方库,例如 boost [http://www.boost.org] 拥有一整套独立于平台的功能。您肯定想查看 boost 为您提供的选项。还要查看其他开源网站,例如 github 等。
  5. 不要养成使用特定于平台的库的习惯。我知道它们有时会让生活变得更轻松,但是当您需要在不同平台上使用代码时,这会带来更多的补偿。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文