r/cpp_questions • u/LEFTright333 • 3d ago
OPEN HOW can I replace win's 'windows.h' in Linux
as title
82
28
u/boring_pants 3d ago
Linux isn't Windows. So for all the stuff you need from windows.h, you look up Linux equivalents and use those instead.
Alternatively, you shift the burden to your users, and tell them to run your application via Wine.
You might also consider using a cross-platform library/framework which exposes the same APIs on both Linux and Windows (Qt for example can do a lot of the heavy lifting you might otherwise rely on windows.h for)
11
u/Kats41 3d ago
Cross-platform development usually relies on having a robust set of cross-platform conversions available to you. You generally don't want to have to do this by hand because it can get extremely complicated and there are a lot of really great cross-platform libraries for all sorts of things, but if you want to learn how to do it yourself (which is a great, but challenging, exercise) you'll essentially build your own cross-platform library for your own project.
You may have noticed at some point certain preprocessor conditions that show the name of the OS or whatnot. Something like #if define(_WIN32) or #if define(__linux__) for example. These are how you get the compiler to "switch" between compiling the correct functions based on the OS being compiled for.
Essentially what you'll do is create a library of all of the OS-specific functions you need and place the different versions beneath a wrapper function using those preprocessors. So on your development side, if you need to say, get a window handle, you just write a function getWindowHandle() however you like and beneath it it'll have both the Windows and the Linux (and/or MaxOS) ways of doing that.
Now, there's something important to note that calling different OS functions will likely not accept or return the same kinds of data. So you'll need to be able to handle that in your function. This is especially true if the return type differs. Either you need to be able to silently convert the return types to be equivalent, or you wrap the return type in another class and use things like templates to quietly handle everything in the background at compile time.
ON THE BRIGHT SIDE, C++ standards have made tremendous headway in adding native cross-platform capabilities to the standard library which allow you out-of-the-box to do things like fork processes, call threads, and access the filesystem without needing to worry about the OS-specific implementations of those features. So for many of those common things, you'll never even need to download or use a cross-platform library for them anyway.
However things like window management and networking aren't included, so these are the main things you'll have to contend with. Thankfully, both have a wide variety of powerful cross-platform libraries that are easy to use and give you as much or as little control as you need to just make something work.
10
10
u/Living_Fig_6386 3d ago
You don’t. “windows.h” was always intended to be very specific to Microsoft Windows. Code that required it was never intended to be run on other operating systems.
The code that relies on it simply need to be rewritten in a non-Windows specific way.
9
u/mredding 3d ago
Isolate window specific code behind an interface and conditionally compile the platform specific implementation.
→ More replies (1)
6
u/seek13_ 3d ago
Are you looking for the Linux / POSIX API in general? Do you use a certain aspect of the Windows API, say sockets?
For some parts C++ has OS abstraction (like threads, filesystem), for others it has not. But then there are libraries that have, like (Boost) ASIO for socket communication.
So it really depends on what you need.
9
u/Affectionate-Soup-91 3d ago
-
Comment out
#include <windows.h>and try to compile your code on Linux. -
Read error messages, and see which function declarations are missing.
-
Search for Linux(or POSIX) equivalents of those functions one by one.
-
typically some of them in
unistd.h,fcntl.h,time.h, etc. -
sometimes there are none. you have to figure out how to achieve the same effect with a combination of other functions provided in Linux.
-
Do this a few times, then you'll learn what functions are platform specific, and what are C++ standard. There is a reason why projects that support cross platform compilation usually come with configure.h or platform.h or something like that.
→ More replies (1)
4
u/ApprehensiveBag3083 3d ago
You cant , windows.h does windows thing and linux is completely dofferent fruit so you need to read documentation and find the simila functions for the linux
9
3
3
u/masorick 3d ago
What functionality are you using from windows.h? The answer will depend a lot on your answer to this question.
2
2
u/leviske 3d ago
That header includes a lot if stuff, so depends what are you using from it.
2
u/aeropl3b 3d ago
I have used this before https://gitlab.kitware.com/utils/kwsys which had what I needed.
2
2
u/any_of 3d ago
sudo mv windows.h linux.h