Windows.h??

I am making a program in Linux that is designed to run in Windows. I had only started programming on Linux earlier this day. I am using the default g++ compiler. When I try to compile my program, I get an error:
1
2
free_roam.cpp:7:21: fatal error: Windows.h: No such file or directory
compilation terminated.

Can I simply download Windows.h? Is there is a package I didn't install? Is there no possible way for me to get Windows.h?

If you're wondering, I cannot simply use another header, as very much of my code uses that header, and (as far as I know) there is no alternative.
You can use MinGW on Linux and cross compile to get a windows executable file. MinGW comes with a windows.h header file so that will work.

If you are compiling for Linux you can't use windows.h.


If you are compiling for Linux you can't use windows.h.


It is kinda weird to have to point this out...
Cross-compiling from linux, especially if you're using a debian based distribution like Ubuntu, is a piece of cake! You just need to install minGW first...
sudo apt-get install mingw32
Then write code like you normally would, using standard libraries only (iostream, vector, string, cstdio/lib, etc.) Compiling on linux is the same as it always is:
g++ -s -o output main.cpp
and then to produce a Windows executable...
i586-mingw32msvc-g++ -s -o output.exe main.cpp
Don't worry about trying to remember the minGW compiler's long name. Typing i5, then hitting tab, and then typing g++ will fill it out for you. Remember that only the standard libraries are portable. If you try to build something using unistd.h, or if you wanted to functionality only available from windows.h, you're out of luck.

Note that executables produced by minGW will be fairly large (about half a megabyte if you use the -s flag). This is because all of the libraries that might be dynamically linked when built with g++ or in visual studio, are built into the executable. This means that the .exe minGW builds will run on all Windows systems without other dependencies, where executables built by visual studio typically don't work correctly without installing a run time on the client's system.
If you try to build something using unistd.h, or if you wanted to functionality only available from windows.h, you're out of luck.

Not really, you can use the WinAPI just fine when you compile with MinGW. It just prevents you from creating a native Linux build.
Wow. I'm...really surprised I didn't know that. Just tried it now, and I can definitely include windows.h and compile with minGW in linux. Test window application works exactly as expected.

EDIT: Programs built this way spawn a console window when they launch. I don't know if it can be suppressed, but a program as trivial as
1
2
3
4
5
6
#include <windows.h>

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmd, int show){
    MessageBox(NULL, "test message","Test",MB_OK);
    return 0;
}

will spawn an empty console every time it's run.

EDIT EDIT:
i586-mingw32msvc-g++ -mwindows main.cpp
Last edited on
Topic archived. No new replies allowed.