r/cpp_questions • u/LEFTright333 • Oct 24 '26
OPEN HOW can I replace win's 'windows.h' in Linux
as title
93
u/saf_e Oct 24 '26
Shortly: you can't
Longer: you need to search for the functionality you need and use Linux counterparts. Better solution would be to use cross platform libraries, which already solved all this hustle by people who knows what they are doing.
9
u/Afraid-Locksmith6566 Oct 24 '26
why do you assume they know what they doing?
16
u/LeeHide Oct 24 '26
In the C++ world, nobody knows what they're doing, so everyone knows what they're doing.
Also a lot of large libraries are utter shit, but they do work
1
u/Afraid-Locksmith6566 Oct 24 '26
i often use an example to ilustrate how software is made: imagine you pay someone to build an apartment building, then after building is finished you do an inspection and what you found is that the contractor put a clausule "the building upon finished construction is provided as is and if it falls apart screw you", and each floor has a safety net that will stop all floors from above from falling onto it, however it all is not a problem as they build it on a swamp
86
20
u/alfps Oct 24 '26
Do explain what you think you need it for. Some of the functionality is already there in the C++ standard library.
28
u/boring_pants Oct 24 '26
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 Oct 24 '26
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.
11
u/Business-Weather-217 Oct 24 '26
use wine++ /s
6
1
1
u/QuraToop314 Oct 24 '26
That’s right. I use Wine myself when I’m doing cross-platform development, as I only use Linux. Once the software is ready (and the IDE hasn’t had a panic attack because of all the errors – since it can generate code completions /s), I test it at my leisure in a virtual machine. And it's work's fine. But only for portability
10
u/Living_Fig_6386 Oct 24 '26
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 Oct 24 '26
Isolate window specific code behind an interface and conditionally compile the platform specific implementation.
2
u/conundorum Oct 25 '26
This is the way. As long as the platform-specific code is hidden behind your own wrapper & glue code, it doesn't matter whether the backend is WinAPI, Linux Kernel, QT, or some weird guy that lives in a swamp.
6
u/seek13_ Oct 24 '26
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 Oct 24 '26
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.
1
u/conundorum Oct 25 '26
And after that, provide a macro block to load the right headers for the platform.
4
u/ApprehensiveBag3083 Oct 24 '26
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
4
9
3
3
u/masorick Oct 24 '26
What functionality are you using from windows.h? The answer will depend a lot on your answer to this question.
2
u/DDDDarky Oct 24 '26
You mean how to use an os api on a completely different os? Yeah gl with that...
2
u/leviske Oct 24 '26
That header includes a lot if stuff, so depends what are you using from it.
If I were you and if my goal would be to have a multiplatform solution without reworking all the business logic, I'd start with creating a wrapper class that implements an expected behavior and hide the platform specific headers in that. If it works with windows.h you can start implementing the Linux version. It makes testing easier as well.
2
u/aeropl3b Oct 24 '26
I have used this before https://gitlab.kitware.com/utils/kwsys which had what I needed.
2
1
1
1
u/phylter99 Oct 24 '26
Wine can provide the header you’re looking for. Go look at the Wine project and see what you can come up with.
Ultimately, you’re better off rewriting anything that depends on windows.h.
1
u/jwezorek Oct 24 '26
If you are looking at code that is including windows.h you are looking at code for a Windows application. There is no "getting it to build on Linux". Building it for Linux typically means rewriting it.
I say "typically" above because is possible that it is making trivial use of the Win32 API for things for which there are POSIX etc. equivalents, but you have not told us enough to make that determination. If it is a GUI application codebase then yeah you need to rewrite it using some other GUI framework e.g. Qt rather than raw Win32.
1
1
u/Liam_Mercier Oct 26 '26
You need Linux specific syscall headers, and will need to replace the calls as well. One way to do this is to use macro's to change which call you include and compile based on the platform.
1
1
u/gm310509 Oct 24 '26 edited Oct 24 '26
Why? To do what?
this sounds like an X-Y Problem.
I mean you could, for example do this:
cd /path_to_where_you_want_to_include_windows.h_from
rm windows.h
touch windows.hI suspect that isn't going to satisfy whatever need you have, but you have just replaced "windows.h" as per your post's title.
1
u/LogicalPerformer7637 Oct 24 '26
no way. just do not use it.
what is not asked, but implied: you must use linux way of interacting with system. you cannot use windows specific function anymore.
1
0
2
u/any_of Oct 24 '26
sudo mv windows.h linux.h