69

I was searching for the source code of the C standard libraries. What I mean with it is, for example, how are cos, abs, printf, scanf, fopen, and all the other standard C functions written, I mean to see their source code.

So while searching for this, I came across with GLIBC, but I don't know what it actually is. It is GNU C Library, and it contains some source codes, but what are they actually, are they the source code of the standard functions or are they something else? And what is it used for?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 5
    [GNU C Library](http://en.wikipedia.org/wiki/Glibc) - it is, as the wiki states, GNU's implementation of the C standard library (with additional stuff that's specific to the GNU implementation). – wkl Jul 12 '12 at 21:16

5 Answers5

79

Its the implementation of Standard C library described in C standards plus some extra useful stuffs which are not strictly standard but used frequently.

Its main contents are :

1) C library described in ANSI,c99,c11 standards. It includes macros, symbols, function implementations etc.(printf(),malloc() etc)

2) POSIX standard library. The "userland" glue of system calls. (open(),read() etc. Actually glibc does not "implement" system calls. kernel does it. But glibc provides the user land interface to the services provided by kernel so that user application can use a system call just like a ordinary function.

3) Also some nonstandard but useful stuff.

"use the force, read the source "

$git clone git://sourceware.org/git/glibc.git

(I was recently pretty enlightened when i looked through malloc.c in glibc)

Marco A.
  • 43,032
  • 26
  • 132
  • 246
Aftnix
  • 4,461
  • 6
  • 26
  • 43
  • 1
    Is there anything else that provides "glibc provides the user land interface to the services provided by kernel"? C is the only language that provides such API? – Marat Mkhitaryan Jan 09 '20 at 08:58
16

There are several implementations of the standard. Glibc is the implementation that most Linuxes use, but there are others. Glibc also contains (as Aftnix states) the glue functions which set up the scene for jumps into the kernel (also known as system calls). So many of glibc's 'functions' don't do the actual work but only delegate to the kernel.

To read the source of Glibc, just google for it. There are myriad sites which carry it, and also several variations.

Windows uses Microsoft's own implementation, which I believe is called MSVCR.DLL. I doubt that you will find the source code to that library anywhere. Also note that some functions which a Linux hacker might think of as 'standard', simply don't exist on Windows (notably fork). The reverse is also true.

Other systems will have their own libc.

jforberg
  • 6,537
  • 3
  • 29
  • 47
  • 5
    Microsoft ships the source code for the C runtime with Visual Studio (even with the free Express version , I believe). However, it does *not* include the source to the floating point routines (or at least a fair chunk of them) for some reason. Maybe they're licensed from a 3rd party that doesn't permit source distribution maybe? I'd be surprised if MS considered them a more important piece of IP than other parts of the runtime. – Michael Burr Jul 12 '12 at 23:00
11

The glibc package contains standard libraries which are used by multiple programs on the system. In order to save disk space and memory, as well as to make upgrading easier, common system code iskept in one place and shared between programs. This particular package contains the most important sets of shared libraries: the standard C library and the standard math library. Without these two libraries, a Linux system will not function. The glibc package also contains national language (locale) support.

Sandy
  • 111
  • 2
  • 2
3

Yes, It's the implementation of standard library functions.

More specifically, it is the implementation for all GNU systems and in almost all *NIX systems that use the Linux kernel.

ardent
  • 2,453
  • 1
  • 16
  • 15
3

Here are a few "hands-on" points of view:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985