What should I use instead of windows.h in Linux?

83,146

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
83,146

Related videos on Youtube

Skeith
Author by

Skeith

Updated on February 11, 2021

Comments

  • Skeith
    Skeith over 3 years

    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.

    • josesuero
      josesuero over 12 years
      You haven't said what your code does, so no. it may surprise you, but describing all the differences in programming Windows vs Linux requires more space than a SO answer typically contains. There isn't a magical ConvertWindowsToLinux() function. They're different OS'es with different APIs. In order to do something using the Linux APIs (POSIX, really), you'll need to start by defining what you want to do.
    • Vijay Kumar Kanta
      Vijay Kumar Kanta over 10 years
      If you want GUI programming in Linux, it's called "X" programming, and it's nowhere related to Win32.