-2

The header files of C library functions can be found under /usr/include. The actual source files, however, don't seem to be simply located anywhere in the file system.

So what exactly happens under the hood if I call a function from a header file from /usr/include in my C program?

cssdev
  • 59
  • 7
  • We'll need some focus here. – nicomp Jul 12 '21 at 15:25
  • 4
    The source code has been precompiled into a *library* which is linked to your program. See https://stackoverflow.com/questions/924485/whats-the-difference-between-a-header-file-and-a-library – Nate Eldredge Jul 12 '21 at 15:26
  • 2
    Have you looked at what's in `/usr/lib`? – Fred Larson Jul 12 '21 at 15:26
  • 1
    Does this answer your question? [What's the difference between a header file and a library?](https://stackoverflow.com/questions/924485/whats-the-difference-between-a-header-file-and-a-library) – Yunnosch Jul 12 '21 at 15:29
  • 1
    Look at the community wiki answer at the proposed duplicate. The accepted answer is not close enough to your question. – Yunnosch Jul 12 '21 at 15:30
  • @Yunnosch: The link is helpful but it doesn't give tangible examples of libraries. What's the name of the C standard library file? (If it matters, I run the current version of Ubuntu) – cssdev Jul 12 '21 at 15:44

1 Answers1

2

The implementation of the C library is typically stored on the system as a shared library which typically has a .so extension. These libraries typically live in /usr/lib, although they can reside in other locations based on the system.

When your program is compiled and linked, it is automatically linked to the C standard library. Then when it runs, it loads the shared libraries that are linked with it.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks! What's the name of the shared library file for the c standard library? If it changes over time, I run the current version of Ubuntu. – cssdev Jul 12 '21 at 15:39
  • 1
    @cssdev It's likely `glibc.so` on your Ubuntu system. Look in `/usr/lib` for shared objects with `libc` in the name. The standard one is generally a symbolic link to a specific version. – h0r53 Jul 12 '21 at 15:40
  • 1
    The "run time c library" is `libc.so.6` . – Knud Larsen Jul 12 '21 at 15:47
  • @h0r53: I don't have "glibc.co". The only file matching "libc*" in /usr/lib/directly is "libcue.so.1 -> libcue.so.1.0.4". Is it this? – cssdev Jul 12 '21 at 15:51
  • @KnudLarsen: I don't have a file ending in ".6" but I have /usr/lib/x86_64-linux-gnu/libc.so which seems similar enough. So that's it probably. – cssdev Jul 12 '21 at 15:54
  • @cssdev yes that's it. It could very well be a symbolic link as well. – h0r53 Jul 12 '21 at 15:56
  • 1
    Debian based OS's like Ubuntu : `/lib/x86_64-linux-gnu/libc.so.6` https://packages.ubuntu.com/focal/amd64/libc6/filelist ...... Else `/lib64/libc.so.6` – Knud Larsen Jul 12 '21 at 16:00
  • Thanks! I only looked in /usr/lib, not in /lib. Now I've found it. – cssdev Jul 12 '21 at 16:03