old-www/HOWTO/From-PowerUp-To-Bash-Prompt...

96 lines
3.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>From Power Up To Bash Prompt: The GNU C Library</TITLE>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO-6.html" REL=next>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO-4.html" REL=previous>
<LINK HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc5" REL=contents>
</HEAD>
<BODY>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-6.html">Next</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-4.html">Previous</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc5">Contents</A>
<HR>
<H2><A NAME="s5">5. The GNU C Library</A></H2>
<P>The next thing that happens as your computer starts up is that init is loaded
and run. However, init, like almost all programs, uses functions from libraries.
<P>
<P>You may have seen an example C program like this:
<P>
<P>
<PRE>
main() {
printf("Hello World!\n");
}
</PRE>
<P>The program contains no definition of <CODE>printf</CODE>, so where does it come from?
It comes from the standard C libraries, on a GNU/Linux system, glibc.
If you compile it under Visual C++, then it comes from a Microsoft
implementation of the same standard functions. There are zillions of
these standard functions, for math, string, dates/times memory allocation
and so on. Everything in Unix (including Linux) is either written in C
or has to try hard to pretend it is, so everything uses these functions.
<P>
<P>
If you look in <CODE>/lib</CODE> on your linux system you will see lots of files called
<CODE>libsomething.so</CODE> or <CODE>libsomething.a</CODE> etc. They are libraries of these functions.
Glibc is just the GNU implementation of these functions.
<P>
<P>There are two ways programs can use these library functions. If you <EM>statically</EM>
link a program, these library functions are copied into the executable that gets
created. This is what the <CODE>libsomething.a</CODE> libraries are for. If you
<EM>dynamically</EM> link a program (and this is the default), then when the program
is running and needs the library code, it is called from the <CODE>libsomething.so</CODE>
file.
<P>
<P>The command <CODE>ldd</CODE> is your friend when you want to work out which
libraries are needed by a particular program. For example, here are the
libraries that <CODE>bash</CODE> uses:
<P>
<P>
<PRE>
[greg@Curry power2bash]$ ldd /bin/bash
libtermcap.so.2 => /lib/libtermcap.so.2 (0x40019000)
libc.so.6 => /lib/libc.so.6 (0x4001d000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
</PRE>
<P>
<H2><A NAME="ss5.1">5.1 Configuration</A>
</H2>
<P>Some of the functions in the libraries depend on where you are. For example, in Australia we write dates as dd/mm/yy, but Americans write mm/dd/yy. There is a program that comes with the <CODE>glibc</CODE> distribution called <CODE>localedef</CODE> which enables you to set this up.
<P>
<H2><A NAME="ss5.2">5.2 Exercises</A>
</H2>
<P>Use <CODE>ldd</CODE> to find out what libraries your favourite applications use.
<P>
<P>Use <CODE>ldd</CODE> to find out what libraries <CODE>init</CODE> uses.
<P>
<P>Make a toy library, with just one or two functions in it. The program
<CODE>ar</CODE> is used to create them, the man page for <CODE>ar</CODE> might be a
good place to start investigating how this is done. Write, compile and link
a program that uses this library.
<P>
<P>
<H2><A NAME="ss5.3">5.3 More Information</A>
</H2>
<P>
<UL>
<LI> source code, see
<A HREF="http://www.netspace.net.au/~gok/power2bash">Building a Minimal Linux System from Source Code</A>
for urls</LI>
</UL>
<P>
<P>
<P>
<HR>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-6.html">Next</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO-4.html">Previous</A>
<A HREF="From-PowerUp-To-Bash-Prompt-HOWTO.html#toc5">Contents</A>
</BODY>
</HTML>