Where is stddef.h defined in Linux?

5,103

The C standard does not distinguish compiler features from library features. The distinction is an implementation detail and can vary from platform to platform, but there are common trends. For example the size of basic integer types such as size_t and associated macros such as CHAR_BIT and SIZE_MAX are properties of the compiler and compiler options; on the other hand the contents of stdio.h are usually independent of the compiler but dependent on how the standard library implements files.

stddef.h mostly declares compiler things, so it comes with the compiler. You'd better get different definitions if compiling in 32-bit or 64-bit mode, for instance, and you'll get different definitions with e.g. GCC and Clang. So look for it in compiler directories. Some compilers even don't have a disk file at all, they just treat the name <stddef.h> specially, but with GCC and Clang you do get a disk file.

You can find all the copies on your system with the locate command.

If you want to know what include path GCC is using when given particular options, pass the option -v on the command line in additions to the other options you use in your build (especially -m).

If you just want this information without compiling anything, invoke GCC in preprocessor-only mode with empty input (-xc -E /dev/null; you need a -xc to specify the language is c because GCC can't tell what language you're compiling without a file name).

The same options work with Clang, by the way.

gcc -v -xc -E /dev/null 2>&1 |
awk ' /^End of search list/ {exit} p {print} /^#include <\.\.\.> search starts here:/ {p=1}' |
xargs sh -c 'for x; do if [ -e "$x/stddef.h" ]; then echo "$x/stddef.h"; exit; fi; done' sh

If all you want to know is the definition of size_t, then you don't need to know where it comes from. Ask the compiler to print the output of the preprocessor.

echo '#include <stddef.h>' | gcc -xc -E - | grep size_t

From man gcc,

  • -E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don't require preprocessing are ignored.
  • -x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option.
Share:
5,103

Related videos on Youtube

Brandon Condrey
Author by

Brandon Condrey

Consider opposing apartheid in Palestine and signing onto the BDS Movement; #1 User for DBA.SE 2017. Available for contracting: 281.901.0011 PostgreSQL &amp; PostGIS / MySQL / SQL Server JavaScript, Typescript, Rx.js, Node.js, Angular Also: C / Perl / Python / Rust / x86 Assembly

Updated on September 18, 2022

Comments

  • Brandon Condrey
    Brandon Condrey almost 2 years

    If I want to find the values of stddef.h, where is it defined? The /usr/include/linux/stddef.h almost has nothing,

    /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
    #ifndef __always_inline
    #define __always_inline __inline__
    #endif
    

    Specifically, I wanted to see how size_t is defined?

    Coming back to this very same question months later wanting to know how whcar_t is defined.

    Note, the internal stddef.h file has the typedef's for ,

    • ptrdiff_t
    • max_align_t
    • size_t
    • wchar_t
  • Thomas Eding
    Thomas Eding almost 3 years
    Thanks for the command to ask the compiler what it uses!