C Library

From OSDev.wiki
Revision as of 15:12, 5 September 2012 by osdev>Bewing (Added clarification of the term "freestanding")
Jump to navigation Jump to search
This page is a stub.
You can help the wiki by accurately adding more contents to it.

The C standard library provides basic functionality such as string manipulation, basic I/O, and memory allocation.

There are three situations where you are likely to want to have one:

  1. If you want to have some traditional C functions that you can use inside your kernel (especially printf), then you will need to link a C library into your kernel when you build it.
  2. Most applications in userspace expect to be able to access traditional C support functions. These functions are usually supplied to userspace in the form of a "runtime C library", that can be dynamically linked into user applications at runtime.
  3. The C library header files are used extensively during C and C++ compilations.

Versions

The C standard describes two versions of each C library: the complete "hosted" library and the "freestanding" one. The so-called "freestanding library" consists of a handful of header files, which contain only defines and typedefs and are used only during compilations.

The __STDC_HOSTED__ macro expands to 1 on hosted implementations, or 0 on freestanding ones. The freestanding headers are: <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>.

GCC defaults to a hosted implementation, but can switch to a freestanding one with the -ffreestanding argument.

Implementations

There exists a myraid of implementations of the standard library avalible at your disposal. There are several open-source C library packages avaialble, and using one may be a viable solution for you. All of them will require some degree of modification to suit your needs. You may instead wish to roll your own, but keep in mind that a good C library implementation often requires 10 years or more to create.

There is a comparison table of some of these at: [1]

Newlib

  • The license is unrestricted (not GPL or LGPL), but each file likely has a different copyright notice.
  • Requires threading, so is more appropriate for a runtime library
  • newlib website

GlibC

Solar's PDCLib

  • Under active development, and not at full working release 1.0 yet
  • Creative Commons Zero license (basically public domain)
  • Good for linking into kernels
  • PDCLib website

microClibC

Musl

diet libc

Caprice

Google's Bionic

  • BSD license (basically public domain)
  • no support for locales
  • no support for wide chars (i.e. multi-byte characters)
  • no libthread_db or libm implementation
  • its own smallish implementation of pthreads based on Linux futexes
  • support for x86, ARM and ARM thumb CPU instruction sets and kernel interfaces

Standards

Especially if you want to roll your own C lib, you may want to buy the ISO/IEC 9899 specification to work from. It is not free. Expect a PDF to cost somewhere around $250 (US) or 250 Swiss Francs, depending on currency conversions.

The older standards (C89/C90, C99) are not commercially available anymore. To find the current standard, go to one of the following sites and search for document "ISO/IEC 9899".