C语言 windows.h 没有这样的文件或目录(在 linux 上编译 c 代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41111050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:32:04  来源:igfitidea点击:

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

c

提问by Joja

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

我有一个包含 header 的 ac 程序。当我使用以下命令编译代码时,该程序在 Windows 上运行良好,但在 linux 上运行良好:

gcc main.c -Wall -o main

I get:

我得到:

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

main.c:2:10: 致命错误 windows.h: 没有这样的文件或目录编译终止

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

您知道为什么会发生此错误以及如何解决吗?

回答by skrrgwasme

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.

问题在于您的代码正在使用 windows.h 头文件来获取仅适用于 Windows 的函数的函数声明。该文件在 Linux 上通常不存在,因为它安装的工具链(例如 GCC)将(默认情况下)仅包含为 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-compileyour code. This will result in a binary that won't work on Linux, but willwork on Windows.

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

  1. 正如 Ed Heal建议的那样,将代码移植到 Linux。这意味着您将删除 windows.h 的包含,并将使用 Windows API 的所有函数调用替换为它们的 Linux 等效项。这将使您的源代码只能在 Linux 上运行,除非您可以将依赖于操作系统的调用重构为与平台无关的代码。一句警告:除非您正在使用的程序是微不足道的,否则这不是一件容易的事。不能保证每个 Windows API 函数都有一个 Linux 等效函数。

  2. 为您的构建系统安装一个 Windows 工具链,其中应包含 windows.h,并交叉编译您的代码。这将导致二进制文件不能在 Linux 上运行,但可以在 Windows 上运行。

  3. 这两个选项之间的中间立场是实际上两者都做,并使用条件编译来允许您有选择地为一个或另一个目标编译。