C ++ Windows和Linux之间的可移植性 [英] C++ Portability between Windows and Linux

查看:211
本文介绍了C ++ Windows和Linux之间的可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于编写程序在Windows和linux之间移植的问题。

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

最近我意识到如果你编写一个程序使用任何类型的外部库,如果那个库没有linux版本(或者在linux下开发的时候是windows版本),那么你会感到不安。

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.

这里是我的问题:如果我在linux中编写一个链接到lol.a的程序,然后我想在Windows上编译并运行它而不将lol.a重新编译为lol.lib,可以像MinGW或Cygwin这样做吗?在Windows平台上链接到.a文件以生成一个Windows可以运行的.exe文件。

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?

推荐答案

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

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.

特别是,CygWin不能运行linux程序。在所有。 CygWin在你的程序和Windows内核之间只提供一个posix兼容层。

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

这种情况在linux上有点不太明显。 Wine可以运行一些本地Windows代码(无需重新编译任何东西,包括原始代码)。但是葡萄酒也有局限性。它不是一个完整的Windows API,并且库代码需要运行的任何内容必须在Wine上可用,否则它也不会工作。对于许多简单的应用程序,这不是一个主要的问题,但许多较新的Windows API的,一些黑暗的角落,老的,没有看到很多用途,特别是任何硬件特定的,可能不会可用。

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.

这篇关于C ++ Windows和Linux之间的可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆