在 linux 中,Win32 API 的等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14161656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:12:16  来源:igfitidea点击:

What would be the equivalent of Win32 API in linux?

linuxapiwinapi

提问by m4l490n

I don't want to know if there is a one-to-one equivalence between API functions in windows and linux, neither want to know every API function.

我不想知道windows和linux中的API函数是否存在一对一的等价关系,也不想知道每个API函数。

I just want to know this for two basic things:

我只想知道这两个基本的事情:

  1. I want to understand why is Qt platform independent
  2. I want to know what API should I use in linux to port an application programmed with the Win32 API, or in other words, at Win32 API level.
  1. 我想了解为什么 Qt 平台独立
  2. 我想知道我应该在 linux 中使用什么 API 来移植使用 Win32 API 编程的应用程序,或者换句话说,在 Win32 API 级别。

I know that this is not practical but I want to know this equivalence.

我知道这不切实际,但我想知道这种等效性。

采纳答案by Basile Starynkevitch

You need to understand what syscallsare. On Linux, they are the lowest possible userland API (in contrast Win32 APIprobably mixes real kernel syscalls with some libraries functions. libcalso does such mix on Linux). fork(2), execve(2), open(2), pipe(2), mmap(2), read(2), poll(2), close(2), dup2(2), sigaction(2)are important syscalls (but there are about 300 of them).

您需要了解系统调用是什么。在 Linux 上,它们是最低可能的用户空间API(相比之下,Win32 API可能将真正的内核系统调用与一些库函数libc混合在一起。在 Linux 上也进行了这种混合)。fork(2)execve(2)open(2)pipe(2)mmap(2)read(2)poll(2)close(2)dup2(2)sigaction(2)是重要的系统调用(但大约有 300 个)。

Don't expect each windows functionality to be available on Linux (and vice versa). Don't even think of such an equivalent. Get a different mindset on Linux. (In particular, processesare very different on Linux and on Windows).

不要指望每个 Windows 功能都可以在 Linux 上使用(反之亦然)。甚至不要考虑这样的等价物。在 Linux 上获得不同的心态。(特别是,Linux 和 Windows 上的进程非常不同)。

Don't forget that Linux is free software, and you can dive into the source code of every function you are using on Linux. Read it, search it, improve it.....

不要忘记 Linux 是免费软件,您可以深入了解您在 Linux 上使用的每个功能的源代码。阅读它,搜索它,改进它......

Read the intro(2)man page first, and several other man pages (notably syscalls(2), intro(3)etc...). Read also e.g. Advanced Linux Programmingand Advanced Unix Programming.

首先阅读intro(2)手册页,以及其他几个手册页(特别是syscalls(2)intro(3)等...)。另请阅读高级 Linux 编程高级 Unix 编程

Some libraries try to factor out and provide a common abstraction for both Posix (e.g. Linux) and Windows. In particular Qt(and also Gtk).

一些库试图为 Posix(例如 Linux)和 Windows 提供一个通用的抽象。特别是Qt(还有 Gtk)。

If you are interested about graphical interface, understand the important role of X11(notice that the X11 server is nearest to screen & keyboard; most graphical applications are X11 clients). In 2016, X11 tend to be superseded by Wayland(but you won't notice that implementation "detail" - a really major one - if you code against Qt or GTK)

如果您对图形界面感兴趣,请了解X11的重要作用(注意 X11 服务器最靠近屏幕和键盘;大多数图形应用程序是 X11 客户端)。在 2016 年,X11 往往被Wayland取代(但如果您针对 Qt 或 GTK 进行编码,您将不会注意到实现“细节”——一个非常重要的细节)

If you write an application using only Qt calls (those not documented as being specific to Linux or Windows) in addition of standard C++ functions, it should be source portable from Linux to Windows and vice versa.

如果除了标准的 C++ 函数之外,你只使用 Qt 调用(那些没有记录为特定于 Linux 或 Windows 的调用)编写应用程序,它应该是从 Linux 到 Windows 的源代码移植,反之亦然。

回答by Andrew Aylett

If you want to port an application that uses Win32 calls, your best bet might be to use WineLib. This uses the libraries underpinning Wine, but it's not the same as just running an application using Wine -- you re-compile your application as a Linux application, just using the WineLib shared libraries. It will still look like a Windows application though, unless you then modify the UI layer.

如果要移植使用 Win32 调用的应用程序,最好的办法可能是使用WineLib。这使用了支持 Wine 的库,但这与仅使用 Wine 运行应用程序不同——您将应用程序重新编译为 Linux 应用程序,只需使用 WineLib 共享库。不过,它看起来仍然像一个 Windows 应用程序,除非您随后修改了 UI 层。

As has been said elsewhere in the answers, there's no real direct equivalent to Win32 in Linux -- different bits of Win32 are provided by different components, and sometimes you've got a choice of components. This is possible because some equivalents of parts of Win32 are implemented natively at a lower level -- for example, Win32 provides UI components, equivalents to which are available in either GTK, Qt or any number of other toolkits (like WineLib), which themselves interact with X. Just as you would usually use components from Win32 rather than drawing your own using lower-level API calls, so you'd normally use components from your high-level UI toolkit rather than using X directly.

正如答案中其他地方所说的那样,在 Linux 中没有真正直接等同于 Win32 - Win32 的不同位由不同的组件提供,有时您可以选择组件。这是可能的,因为 Win32 部分的某些等价物是在较低级别本地实现的——例如,Win32 提供 UI 组件,在 GTK、Qt 或任何数量的其他工具包(如 WineLib)中都可用的等价物,它们本身与 X 交互。正如您通常使用来自 Win32 的组件而不是使用较低级别的 API 调用绘制自己的组件一样,因此您通常会使用来自高级 UI 工具包的组件而不是直接使用 X。