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

发布于 2024-12-03 01:10:17 字数 409 浏览 0 评论 0原文

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

我取出了 windows.h 文件,但我确实知道如何处理 CALLBACKHANDLEDWORD > 和 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 技术交流群。

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

发布评论

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

评论(5

青芜 2024-12-10 01:10:17

缺少的 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.

故事灯 2024-12-10 01:10:17

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

但是,如果我们忽略 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 源代码

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, ...):

#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

梦途 2024-12-10 01:10:17

拥有 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.

放低过去 2024-12-10 01:10:17

你有两个选择。

  1. 将您缺少的类型类型定义为适合您的新操作系统的类型。
  2. 重写代码以不使用这些类型。

我怀疑没有人能够在不了解每种情况的具体情况的情况下告诉您应该如何处理该端口。关于 windows.h 的问题是它依赖于 Windows 操作系统...您必须更改从中调用的函数。在这种情况下,您将不再使用您没有的类型。
基本上,您必须了解 Windows API 的用途,并找到适合您的目标操作系统的等效函数。

You have 2 options.

  1. Typedef your missing types to types that are suitable for your new OS.
  2. Rewrite the code not to use those types.

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.

眸中客 2024-12-10 01:10:17

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.

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