52

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;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Louie
  • 673
  • 1
  • 7
  • 11
  • How are you compiling it? – Chol Nhial Jul 24 '15 at 01:13
  • try `g++ file.cc` where file.cc is the name of yoru file (should end in `.cc` or `.cpp`). If you still get the same error then your compiler installation is broken; fire up the Cygwin installer and refresh your installation of g++. Check that `gcc` and `glibc` are up to date also. – M.M Jul 24 '15 at 03:10
  • @Chol Nhial I compile the code with using the command: g++ test.cpp I also tried g++ test.cpp -o main. – Louie Jul 24 '15 at 22:22
  • @Matt McNabb: I've re-installed g++ but it still gives me that error, where can I find the gcc and glibc? – Louie Jul 24 '15 at 22:24
  • In the cygwin installer, same page as g++ – M.M Jul 25 '15 at 05:45
  • I can't find it, am I even looking at the right place? http://i.imgur.com/pyd9FZU.png Edit: fail, I can't post the image, I'll just post a link with it... – Louie Jul 25 '15 at 06:28

7 Answers7

80

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.

thisizkp
  • 1,757
  • 20
  • 21
  • 8
    How is that even possible? How does the setup program manage to install the wrong versions of packages on a fresh install? Also how do you perform the update? I'm running setup again, and there does not appear to be an option to update anything. – jww Oct 23 '15 at 11:07
  • 22
    If you are using [Babun](http://babun.github.io/), `pact update gcc-core gcc-g++`. – anishpatel Oct 28 '15 at 02:27
  • 5
    Still relevant today - installed gcc-core and gcc-g++ for the first time on an existing Cygwin install, and it installed a back level version of gcc-g++, I had to re-run setup.exe and it updated it. – EightBitTony Mar 01 '16 at 17:45
  • cygwin package manager apt-cyg or sage: `sage remove gcc-core gcc-g++ && sage install gcc-core gcc-g++` – U007D Aug 24 '16 at 22:36
  • From **MinGW Instalation Manager** I upgraded *mingw32-gcc-g++ 4.9...* to *mingw32-gcc-g++ 5.3.0-2* and it works! Thank you so much! – alexandre-rousseau Nov 16 '16 at 07:42
  • 2
    If you are using Babun and @nishpatel's answer did not work for you, try also run `pact update cygwin-devel`. – David Hoksza Jan 21 '17 at 18:38
  • As per this answer, just use http://www.mingw.org instead of Cygwin http://stackoverflow.com/questions/43470379/c-not-compiling-in-netbeans?noredirect=1#comment74060877_43470379 – northerner Apr 19 '17 at 20:25
  • Perfect! I had some packages at 7.04 and others at 6.04. When I downgraded to 6.04 it all worked. – melston Apr 16 '18 at 22:59
  • +1. I was having this problem when building packages for Slackware 14.2 through SBopkg tool, found your solution, implemented it, and it worked! – user47 Feb 04 '20 at 13:55
5

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 -nostdinc in 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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Ben
  • 8,725
  • 1
  • 30
  • 48
  • Where is the answer of "Prasanth Karri"? – jw_ Jan 12 '20 at 03:23
  • Probably the still-accepted [answer by "ThisIzKp"](https://stackoverflow.com/a/32410193/1390430). Sorry that past me didn't feel the need to link it. :-( – Ben Jan 12 '20 at 21:49
3

In order to update it, follow below. If you are on Windows, just run these on command prompt or powershell

Update the package list: mingw-get update

After updating the package list, run: mingw-get upgrade

Source: How to update GCC in MinGW on Windows?

Dimenein
  • 281
  • 2
  • 5
2

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.

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 #include the 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 #include of a module that actually does not exist (within its scope), as it's not a C++ compiler.

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.

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.

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.

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

Gilrain
  • 106
  • 5
0

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

Felix Lemke
  • 6,189
  • 3
  • 40
  • 67
  • 1
    Welcome to SO, we appreciate your input! Please edit your question so that it explains why it might be working. For more, see https://stackoverflow.com/help/how-to-answer – B--rian Aug 14 '19 at 06:59
0

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

Yonina Ab
  • 9
  • 2
0

After installing the C++ compiler with MinGW I encountered this problem as well. Apparently, you have to also install mingw32-base. Go to C:/MinGW/bin/mingw-get.exe (my path) and check it for installation at the Basic Setup tab.

georbbtz
  • 109
  • 1
  • 2