10

I have compiled the gnu standard library and installed it in $GLIBC_INST.

Now, I try to compile a very simple programm (using only one #include : #include <stdio.h>):

gcc --nostdinc -I$GLIBC_INST/include foo.c

The compilation (preprocessor?) tells me, that it doesn't find stddef.h.

And indeed, there is none in $GLIBC_INST/include (nor is there one in /usr/include). However, I found a stddef.h in /usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/include.

Why is that file not under /usr/include? I thought it belonged to the standard c library and should be installed in $GLIBC_INST/include.

How can I compile my foo.c with the newly installed standard library when it doesn't seem to come with a stddef.h?

Edit: Clarification

I feel that the title of this question is not optimal. As has been pointed out by some answers, there is not a requirement for stddef.h to be in /usr/include (or $GLIBC_INST/include, for that matter). I do understand that.

But I am wondering how I can proceed when I want to use $GLIBC_INST. It seems obvious to me (although I might be wrong here) that I need to invoke gcc with --nostdinc in order to not use the system installed header files. This entails that I use -I$GLIB_INST/include. This is clear to me.

Yet, what remains unclear to me is: when I also add -I/usr/lib/gcc/x86..../include, how can I be sure that I do have in fact the newest header files for the freshly compiled glibc?

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
  • Could you confirm that `gcc` does indeed point to your 5.3.0 installation, e.g., with `which gcc`? – Benjamin Bannier May 11 '16 at 09:42
  • If you add `-I` option the compiler looks first to the added include path before to look into the standard dirs. Using `--nostdinc` you are strongly sure that you are using your freshly installed headers. – LPs May 11 '16 at 10:11
  • Also, see http://stackoverflow.com/questions/31285258/why-usr-include-linux-stddef-h-is-empty – Andrew Henle May 11 '16 at 10:57

3 Answers3

12

That's because files under /usr/include are common headers that provided by the C library, for example, glibc, while the files at /usr/lib/gcc are specific for that particular compiler. It is common that each compiler has their own different implementation of stddef.h, but they will use the same stdio.h when links to the installed C library.

fluter
  • 13,238
  • 8
  • 62
  • 100
3

When you say #include <stddef.h> it does not require that /usr/include/stddef.h exists as a file on disk at all. All that is required of an implementation is that #include <stddef.h> works, and that it gives you the features that header is meant to give you.

In your case, the implementation put some of its files in another search path. That's pretty typical.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
3

Why is that file not under /usr/include?

Because there's absolutely no requirement for standard headers to be located at /usr/include/.

The implementation could place them anywhere. The only guarantee is that when you do #include <stddef.h>, the compiler/preprocessor correctly locates and includes it. Since you disable that with -nostdinc option of gcc, you are on your own (to correctly give the location of that header).

machine_1
  • 4,266
  • 2
  • 21
  • 42
P.P
  • 117,907
  • 20
  • 175
  • 238