I hav been spending some time these days going through gcc internals. I found the collection of libraries that gcc provides for support to our programs.
What is the use of libgcc
(The GCC runtime library.) I mean which are the most commonly used functions of that library?? :-/
And I found that there was a library libiberty
. I found the library incorporates many of the commonly开发者_Python百科 used functions(i mean the routines i use) including alloca
, concat
,and calloc
. But I couldnt find functions similar to theem like malloc
and other string routines. So when we include < string.h >
or < alloc.h >
is it that the header file is linked with two different libraries??
My concepts arent good enogh. :( please help..
libgcc contains auxiliary functions that work around "limitations" of the hardware; for example, 64-bit integer divide is part of libgcc on x86(_32) — the infamous __udivdi3
.
You can think library as the implementation of functions that you can use in your program without bothering to write code for them. For eg: you use 'printf' function, but you don't actually write the code for 'printf'. So in simple terms, libraries are the collection of implementation of commonly used code (or functions).
When you compile and link your program, based on the linking options, your program is linked (statically or dynamically) with other libraries.
Read about static and dynamic linking for more details and better understanding of libraries.
lets take an example:
#include <stdio.h>
#include <math.h> // contains deceleration of sqrt function
int main ()
{
printf ("sqaure root of 4 is %d.", sqrt (4));
}
In this code, we are using the sqrt
function, but we are not implementing it.
Now if we compile and link it with the library containing the definition of sqrt function (math library), our code will work fine at run time.
However, if you don't want to link to the math library, you will have to write your own function for computing sqrt.
compile and link math library by issuing:
gcc file.c -lm
here, -l is used for mentioning that we are going to link a library -lm tells to link the 'm' (or math) library.
For more details, read about linkers.
精彩评论