C++ 编译错误:“stddef.h:没有那个文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31600600/
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-08-28 14:00:06  来源:igfitidea点击:

Compilation error: "stddef.h: No such file or directory"

c++g++cygwin

提问by Louie

Whenever I try to compile this code it always ends up with this error:

每当我尝试编译这段代码时,它总是以这个错误告终:

    In file included from /usr/include/wchar.h:6:0,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/cwchar:44,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/postypes.h:40,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iosfwd:40,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ios:38,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ostream:38,
             from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iostream:39,
             from test.cpp:1:
    /usr/include/sys/reent.h:14:20: fatal error: stddef.h: No such file or directory
    #include <stddef.h>
                ^
    compilation terminated.

The code I was trying to compile is:

我试图编译的代码是:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World! :D";
    return 0;
}

回答by ThisIzKp

The error is because your gcc-core package and gcc-g++ are not of the same version. Either downgrade one of them to solve the problem or update both the libraries. Updating both the libraries is the recommended way.

错误是因为您的 gcc-core 包和 gcc-g++ 不是同一版本。要么降级其中一个以解决问题,要么更新两个库。更新这两个库是推荐的方式。

回答by Ben

I had this error on a fresh MinGW install, it had nothing to do with the installed packages mentioned in the current accepted answer by "Prasanth Karri". In my case the issue was caused by -nostdincin my Makefile. I actually only needed that compiler flag when building for a different target platform (not when using MinGW) so I fixed the issue by removing that flag from MinGW builds.

我在新的 MinGW 安装中遇到了这个错误,它与“Prasanth Karri”当前接受的答案中提到的已安装软件包无关。在我的情况下,问题是由-nostdinc我的 Makefile引起的。我实际上只在为不同的目标平台构建时(而不是在使用 MinGW 时)需要该编译器标志,所以我通过从 MinGW 构建中删除该标志来解决这个问题。

回答by Gilrain

When I was incorporating a software library written in C into an existing demo project(used a C++ mbed library) I encountered this problem. The demo project would compile just fine, but after I replaced the existing main file by my own, this error occurred.

当我将用 C 编写的软件库合并到现有的演示项目(使用 C++ mbed 库)时,我遇到了这个问题。演示项目可以编译得很好,但是在我自己替换了现有的主文件后,出现了这个错误。

At this point I hadn't yet thought about the fact that the mbed library that I needed was written in C++. My own main file was a .c file that #includethe mbed header file. As a result I used my normal C source as if it was a C++ source. Therefore the compiler that was used to compile my main file was the C compiler. This C compiler then encountered a #includeof a module that actually does not exist(within its scope), as it's not a C++ compiler.

在这一点上,我还没有想到我需要的 mbed 库是用 C++ 编写的。我自己的主文件是#includembed 头文件的 .c文件。结果我使用了我的普通 C 源代码,就好像它是一个 C++ 源代码一样。因此,用于编译我的主文件的编译器C 编译器。这个 C 编译器然后遇到了#include一个实际上不存在的模块(在它的范围内),因为它不是一个 C++ 编译器。

Only after I inspected the output of the build log I realised the various source C and C++ files were compiled by more that 1 compiler(the c++ compiler). The project used used compilers arm-none-eabi-c++ and arm-none-eabi-gcc (for embedded systems) as seen below.

只有在我检查了构建日志的输出后,我才意识到各种源 C 和 C++ 文件是由超过 1 个编译器(C++ 编译器)编译的。该项目使用了编译器 arm-none-eabi-c++ 和 arm-none-eabi-gcc(用于嵌入式系统),如下所示。

Compile log:

编译日志:

Building file: ../anyfile.cpp
Invoking: MCU C++ Compiler
arm-none-eabi-c++ <A lot of arguments> "../anyfile.cpp"
Finished building: ../anyfile.cpp

Building file: ../main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc <A lot of arguments> "../main.c"
In file included from <Project directory>\mbed/mbed.h:21:0,
                 from ../main.c:16:
<Project directory>\mbed/platform.h:25:19: fatal error: cstddef: No such file or directory
compilation terminated.

Of course in a C++ environment cstddef exists, but in a C environment cstddef doesn't exist, in stead it's just C's implementation of stddef.

当然,在 C++ 环境中cstddef 存在,但在 C 环境中 cstddef 不存在,它只是 C 对 stddef 的实现

In other words, cstddef does not exist in the C compiler. I resolved this problem by renaming my main.c file to main.cpp and the rest of the code compiled smoothly too.

换句话说,C 编译器中不存在 cstddef。我通过将 main.c 文件重命名为 main.cpp 解决了这个问题,其余代码也顺利编译。

TLDR/Conclusion: When building a C++ project, avoid mixing C files with C++ files(sources and headers). If possible rename .c files to .cppfiles to use the C++ compiler in stead of the C compiler where required.

TLDR/结论:在构建 C++ 项目时,避免将 C 文件与 C++ 文件(源代码和头文件)混合。如果可能,将 .c 文件重命名为 .cpp文件以在需要时使用 C++ 编译器而不是 C 编译器。

回答by Yonina Ab

If you try to compile and see a message like, "fatal error: stddef.h: No such file or directory", the error is because your gcc-core and gcc-g++ packages are not of the same version. Rerun the Cygwin install and make sure that you select the highest numbered versions of gcc-core and gcc-g++.

如果您尝试编译并看到类似“致命错误:stddef.h:没有这样的文件或目录”的消息,则该错误是因为您的 gcc-core 和 gcc-g++ 软件包的版本不同。重新运行 Cygwin 安装并确保您选择 gcc-core 和 gcc-g++ 的最高编号版本。

回答by Ankit Tamboli

This problem was solved for me as I installed codeblockswith mingw compiler then I copied the mingw folder from codeblocks to C drive and added C\mingw\binto the environment variables.

当我安装codeblocks了 mingw 编译器时,这个问题为我解决了,然后我将 mingw 文件夹从代码块复制到 C 驱动器并添加 C\mingw\bin到环境变量中。