在 Linux 中我应该使用什么来代替 windows.h?
我正在尝试将 Windows 程序移植到 Linux。我从来没有在 Linux 中编程过,对自己在做什么知之甚少。我已经设法消除了 Linux 上的 G++ 编译器中出现的许多错误,并将大部分剩余错误追溯到不存在的变量类型。
我取出了 windows.h
文件,但我确实知道如何处理 CALLBACK
、HANDLE
、DWORD
> 和 HHOOK
变量。显然,Linux 中没有相当于 HANDLE
的东西,所以我想我可能必须重写一些结构。
谁能解释一下我应该做什么,或者给我指点一些教程来教我如何在 Linux 中做这些事情?
我的程序在 RS-485 网络上运行轮询循环(如果有帮助的话)。
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.
I took out the windows.h
file but I do know know what to do with the CALLBACK
, HANDLE
, DWORD
and HHOOK
variables. Apparently there is no equivalent to HANDLE
in Linux so I think I may have to rewrite some of the structure.
Can anyone explain what I should be doing or point me to some tutorials that teach me how to do these things in Linux?
My program runs a polling loop on an RS-485 network if that helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
缺少的 typedef(
HANDLE
等)不是您的问题。你的问题是 Linux 和 Windows 具有完全不同的 API,你不能简单地希望通过替换一些类型定义来将一个 API 移植到另一个 API。代码中与平台相关的完整部分必须被替换。因此,您的第一步是学习 Linux API。最好的方法是获取一本有关 Linux 编程的书。
此外,Linux 不像 Windows 那样提供默认的窗口管理 API,因此如果您正在编写图形应用程序,那么您还需要选择一个窗口库。
The missing typedefs (
HANDLE
etc.) 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.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.
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.h 来说,没有“等效项”,您需要逐个修复错误,或者更好的是,为 linux 重写代码(如果不是太复杂的话)。
但是,如果我们忽略 Windows 特定的 API,您也许能够修复 typedef 错误(DWORD、HANDLE,...):
typedef 源代码
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).
However, if we ignore windows specific APIs, you may be able to fix typedef errors (DWORD, HANDLE, ...):
typedef source code
拥有 windows.h 意味着您的应用程序使用 Windows 操作系统的 API,不存在与 Linux 上的库的一对一映射。
如果您不想移植应用程序,可以考虑在 Wine 下运行应用程序。
Having windows.h means that your application uses the API of the Windows operating system, there is no 1-to-1 mapping to libraries on Linux.
You can consider running your application under Wine if you don't want to port the application.
你有两个选择。
我怀疑没有人能够在不了解每种情况的具体情况的情况下告诉您应该如何处理该端口。关于 windows.h 的问题是它依赖于 Windows 操作系统...您必须更改从中调用的函数。在这种情况下,您将不再使用您没有的类型。
基本上,您必须了解 Windows API 的用途,并找到适合您的目标操作系统的等效函数。
You have 2 options.
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.
Linux 的编程模型与 Windows 完全不同。如果您对串行端口编程感兴趣(RS485 类似于 RS232)这里应该向您展示基础知识的教程。
Linux has quite a different programming model to Windows. If you are interest in programming a serial port (RS485 is similar to RS232) here is a tutorial that should show you the basics.