cgccgliblibccc

Where are libc files located


When I write code such as

#include <stdio.h>

int main(int argc, char** argv) {
    printf("Hello, world!\n");
    return 0;
}

gcc imports stdio.h which in turn requires an associated stdio.c where the functions are defined. I know on linux stdio.h is located in /usr/include/stdio.h but where is stdio.c? There is no /usr/include/stdio.c but, obviously, gcc can compile this code just fine.


Solution

  • libc, like most libraries, is compiled ahead of time and shipped with your system as a compiled library rather than as source code. On Linux, that library will be called libc.a (for static linking) or libc.so (for dynamic linking), and can usually be found in /usr/lib or one of its subdirectories.

    Also, note that in C there is actually no requirement that the header file name match that of the source file. For example, glibc, one of the most popular implementations of libc, scatters its stdio implementation across several files (approximately one C file for each function in stdio); these source files can be found here in the glibc git repository.