2

I am not sure why there are so many header file for the C standard library (stdio.h, stdlib.h, math.h).

How do these header files point to the same library?

I guess I am a little bit confused about what actually is a library. Where does it exist? Isn't a library linked to the program through a header file?

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
yoyo_fun
  • 2,267
  • 3
  • 17
  • 22

2 Answers2

8

A header normally contains declarations to tell a compiler about the functions (and classes, etc.) that exist in a library.

Most C and C++ compilers include a standard library that they'll link with by default. To link with other libraries, you typically have to tell the compiler (linker, really, at least in most cases) to link with them.

As to why there are so many headers: mostly because there are lots of declarations, many of which are only minimally related to each other. Since a header (normally) contains source code, each time you compile a file that includes a header, you may also be re-compiling the entirety of that header. This can slow down compilation considerably (and compilation speed is often a problem with C and C++ anyway).

Libraries themselves typically come in two forms: one is a "static library". This is basically just a bunch of object files collected together into a single file, often with a directory of some sort appended to make it easier to find which functions are contained in which files. The linker basically just looks through the directory to find files that will satisfy currently-unresolved external references in your code, and links them in like it would any other object file.

The second is a dynamic library (e.g., DLL on Windows, .so on Linux). This is more like an executable--a set of object files that have been pre-linked into a loadable form. When you link against one, the linker inserts some sort of reference to the dynamic library into your executable. When you execute your program, the OS' loader maps the dynamic library to your process' address space, and "fixes up" the references to the functions in the dynamic library, so they refer to the correct addresses in the library.

Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • 1
    I think I understand now. So the inclusion of header files has no meaning to the linker and to the libraries that the program will be linked to. It only tells the program about the prototype of certain functions that will be used in the program so the program will not be confused. – yoyo_fun Sep 13 '17 at 15:54
  • @yoyo_fun: that's mostly correct. There is one final twist to the story though: some tool chains (Microsoft) allow you to write something into a header that inserts a record into the object file to tell the linker that it should link against a particular library. Boost (for one example) uses this quite a bit, so (on Windows) when you include a header, it'll automatically link against the matching library. See the docs for pragma comment lib, if you care about this. – Jerry Coffin Sep 13 '17 at 16:04
  • thanks for the info. Is it possible in Linux to specify to the linker to link to a particular library from the source code and NOT from the command line? Like these tool chains do, from what I understand? – yoyo_fun Sep 13 '17 at 17:18
  • @yoyo_fun: No, at least not to my knowledge. – Jerry Coffin Sep 13 '17 at 18:46
  • The performance issues are historical. Its been over 2 decades since recompiled headers solved that problem. – mattnz Sep 14 '17 at 03:26
  • 3
    @mattnz: Oh, how I really and truly wish you were correct. – Jerry Coffin Sep 14 '17 at 03:35
2

New Identifiers

C, over the years, has added various new Standard Library functions, types, defines, etc.

It is possible that older code may have used prior some of these new identifiers.

A classic example is bool,true,false defined in the "new" <stdbool.h>.

To prevent a recompilation of that old code with a new compiler from failing, bool,true,false are defined in a now standard include file that did not exist before. Thus the old code would not have included it and no conflict arises.

Below are includes added since C89

<assert.h>
<fenv.h>
<inttypes.h>
<iso646.h>
<stdalign.h>
<stdatomic.h>
<stdbool.h>
<stdint.h>
<stdnoreturn.h>
<tgmath.h>
<threads.h>
<uchar.h>
<wchar.h>
<wctype.h>

Library segmentation

Although the C library could be one large entity, historically it has been in pieces, notable the floating point math has been handled separately owing to its size and non-applicability to many a C program without FP math. Thus there has been some alignment of select header files with some chunks of the standard C library. Code that does not include the header likely does not need that chunk of the library.