Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows.h no such file or directory (compile c code on linux) [closed]

Tags:

c

I have a c program that includes a header . This program works fine on windows but on linux when I compile the code with:

gcc main.c -Wall -o main

I get:

main.c:2:10: fatal error windows.h: No such file or directory compilation terminated

Do you have any idea why this error happens and how to fix?

like image 301
Joja Avatar asked Dec 12 '16 22:12

Joja


People also ask

Does Windows H work on Linux?

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).

What is #include Windows H?

h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems.

Can you compile Windows programs on Linux?

mingw32 exists as a package for Linux. You can cross-compile and -link Windows applications with it. There's a tutorial here at the Code::Blocks forum. Mind that the command changes to x86_64-w64-mingw32-gcc-win32 , for example.

Does GCC have Windows H?

h header file to get function declarations for Windows-only functions. This file does not normally exist on Linux, because its installations of toolchains (such as GCC) will (by default) only include the files needed to compile for Linux. You have a few options: As Ed Heal suggested, port the code to Linux.


1 Answers

The problem is that your code is using the windows.h header file to get function declarations for Windows-only functions. This file does not normally exist on Linux, because its installations of toolchains (such as GCC) will (by default) only include the files needed to compile for Linux.

You have a few options:

  1. As Ed Heal suggested, port the code to Linux. That means you would remove the inclusion of windows.h, and replace all the function calls that used the Windows API with their Linux equivalents. This will make your source code only work on Linux, unless you can refactor the OS-dependent calls into platform-agnostic code. A word of warning: unless the program you're working with is trivial, this is not an easy task. There's no guarantee that every Windows API function has a Linux equivalent.

  2. Install a Windows toolchain for your build system, which should include windows.h, and cross-compile your code. This will result in a binary that won't work on Linux, but will work on Windows.

  3. A middle ground between those two options would be to actually do both, and use conditional compilation to allow you to selectively compile for one target or another.

like image 90
skrrgwasme Avatar answered Oct 05 '22 23:10

skrrgwasme