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-开发者_如何学JAVA485 network if that helps.
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.
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
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.
You have 2 options.
- Typedef your missing types to types that are suitable for your new OS.
- 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.
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.
精彩评论