在 linux 中我应该使用什么来代替 windows.h

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7317523/
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-05 06:02:10  来源:igfitidea点击:

What should i use instead of windows.h in linux

c++linuxporting

提问by Skeith

I am trying to port a Windows program to Linux. I have never programmed in Linux and have very little idea what I am doing. I have managed to eliminate a lot of the errors I got in the G++ compiler on Linux and have traced most of the remaining ones to non existent variable types.

我正在尝试将 Windows 程序移植到 Linux。我从来没有在 Linux 中编程过,也不知道我在做什么。我已经设法消除了我在 Linux 上的 G++ 编译器中遇到的许多错误,并将大部分剩余的错误追溯到不存在的变量类型。

I took out the windows.hfile but I do know know what to do with the CALLBACK, HANDLE, DWORDand HHOOKvariables. Apparently there is no equivalent to HANDLEin Linux so I think I may have to rewrite some of the structure.

我拿出windows.h文件,但我知道知道做什么用的做CALLBACKHANDLEDWORDHHOOK变量。显然HANDLE在 Linux 中没有等价物,所以我想我可能不得不重写一些结构。

Can anyone explain what I should be doing or point me to some tutorials that teach me how to do these things in Linux?

任何人都可以解释我应该做什么或向我指出一些教我如何在 Linux 中做这些事情的教程吗?

My program runs a polling loop on an RS-485 network if that helps.

如果有帮助,我的程序会在 RS-485 网络上运行轮询循环。

采纳答案by Konrad Rudolph

The missing typedefs (HANDLEetc.) aren't your problem. Your problem is that Linux and Windows have completely different APIs, you cannot simply hope to port one to the other by replacing a few type definitions.

缺少的 typedef(HANDLE等)不是您的问题。您的问题是 Linux 和 Windows 具有完全不同的 API,您不能简单地希望通过替换一些类型定义来将一个移植到另一个。

The complete platform-dependent portion of your code has to be replaced. Your first step is therefore to learn the Linux API. The best way of doing this is getting a book on Linux programming.

必须替换代码的完整平台相关部分。因此,您的第一步是学习 Linux API。最好的方法是买一本关于 Linux 编程的书。

Furthermore, Linux doesn't provide a default API for window management as does Windows so if you are programming a graphical application then you need to choose a windowing library as well.

此外,Linux 不像 Windows 那样提供用于窗口管理的默认 API,因此如果您正在编写图形应用程序,那么您还需要选择一个窗口库。

回答by Ahmed Khalaf

windows.h means that your application uses the API of the Windows operating system, there is not 1-to-1 mapping to another libraries on Linux.

windows.h 意味着您的应用程序使用 Windows 操作系统的 API,在 Linux 上没有一对一映射到另一个库。

You can consider running your application under Wine if you don't want to port the application.

如果您不想移植应用程序,可以考虑在 Wine 下运行您的应用程序。

回答by doron

Linux has quite a different programming model to Windows. If you are interest in programming a serial port (RS485 is similar to RS232) hereis a tutorial that should show you the basics.

Linux 具有与 Windows 完全不同的编程模型。如果您对串行端口(RS485 类似于 RS232)的编程感兴趣,这里有一个教程,可以向您展示基础知识。

回答by Dennis

You have 2 options.

您有 2 个选择。

  1. Typedef your missing types to types that are suitable for your new OS.
  2. Rewrite the code not to use those types.
  1. 将您缺少的类型定义为适合您的新操作系统的类型。
  2. 重写代码以不使用这些类型。

I doubt that anyone will be able to tell you what you should do with the port without knowing the specifics of each case. The thing about windows.h is that it relies on the windows OS... you'll have to change the functions that you are calling from it. In that case you will no longer be using the types that you do not have.
Basically you'll have to understand what the windows API is being used for and find the equivalent functions for your target OS.

我怀疑任何人都无法在不知道每个案例的具体情况的情况下告诉您应该对端口做什么。关于 windows.h 的事情是它依赖于 windows 操作系统......你必须改变你从它调用的函数。在这种情况下,您将不再使用您没有的类型。
基本上,您必须了解 Windows API 的用途,并为您的目标操作系统找到等效的功能。

回答by Soufiane Hassou

There's no "equivalent", so to speak, for windows.h in Linux, you need to fix your errors case by case, or better, rewrite your code for linux (if it's not too complicated).

可以这么说,对于 Linux 中的 windows.h 没有“等效”,您需要逐个修复错误,或者更好的是,为 linux 重写代码(如果不是太复杂)。

However, if we ignore windows specific APIs, you may be able to fix typedef errors (DWORD, HANDLE, ...):

但是,如果我们忽略特定于 Windows 的 API,您可能能够修复 typedef 错误(DWORD、HANDLE 等):

#ifndef DWORD
#define WINAPI
typedef unsigned long DWORD;
typedef short WCHAR;
typedef void * HANDLE;
#define MAX_PATH    PATH_MAX
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int BOOL;
#endif

typedef source code

typedef 源代码