Wiki

Clone wiki

minlibd / libc_vs_libgcc

Many programming languages have different libraries. Often there is runtime libraries and utility libraries. There may also be some internal libraries. These libraries seems to confuse many people.

When the basic features of the language has been put together, the rest can be written using the language itself. This code is much easier to write, maintain and port than the low level code used to make the basic set. There is also code that depends on the operating system or the processor used. It is useful to separate this code from the system independent code.

It depends totally on the language how the code is organized. Some of this higher level code is needed to interpret the language. This part may be integrated into the compiler/interpreter or the code may be in a library in a private directory. Some code is needed to run the user program. This code often is in a separate runtime library. This library may be in a public library directory but is not meant for users. There is also code that is not needed by the language itself but is needed to make the language useful for the user. This library is in some public place and is available to users.

libgcc

In GCC there is a common backend for all languages. All operations in all languages are translated so simple language independent operations. The backend contains machine description files that describe the actual machine code in the selected processor. These code patterns have to be very generic and all patterns have to be written for every supported processor. If the code pattern for an operation is bigger than a few instructions it is just easier to call a library function. These functions are in libgcc. One big part of this library is floating point arithmetic or other nuneric operations that require more bits than the processor registers have.

In the C compiler there are also functions called compiler intrinsics. These functions are not part of the language but the compiler can recognize them and use optimized versions of these functions. These are also in libgcc.

Functions in libgcc are not meant for user and its content is not available in header files.

In normql use libgcc is automatically included. When using gcc with -nostdlib or -nodefaultlibs options libgcc is omitted. Also it seems that using a cross compiler with a custom linker script automatically behaves like -nostdlib. In these cases the full path to a correct library must be given. Very simple programs may not need libgcc at all but most of the time there is something that is needed.

Libgcc is located in the gcc internal directory tree. For self compiled gcc this tree is by default /usr/local/lib/gcc but precompiled packages may put this to other place. Under this tree there is a directory for every target and every version. If the target supports multilib there will be one libgcc directory for every multilib target.

Sometimes the used options select a wrong library. These gcc options are useful with libgcc:

-print-multi-directory prints the directory where multi libgcc subdirs are for this particular invocation of gcc.

-print-multi-lib prints available libraries and gcc options that is needed to select each one.

-print-libgcc-file-name prints full path to the libgcc that would be selected with given options.

libc

Libc is the standard c utility function set. It contains basic things like string, math and time functions and higher level things like file access and networking.

Libc is not part of a c compiler but compilers assume that some kind of libc exists. In GNU systems the gnu libc is the standard one. Compiler vendors often provide their own libc. Newlib from Redhat is a free alternative for small systems.

Libc is not required to compile and run a c program and many microcontroller programs do not use it at all.

To be continued...

Updated