cc -o myprog main.c /usr/libraries/stdio.o \ /usr/libraries/strings.oto include the standard I/O and standard string handling functions into the program. (The stdio.o file would be a separately compiled file created from stdio.c, and would contain all the I/O functions such as printf and scanf; and so on.)
cc -c main.cNext, we can combine the main.o file with the required library files by executing:
ld -o myprog /usr/lib/crt0.o main.o /usr/lib/libc.aand this will create an executable file named myprog.
[E.g., if the main.c file has a call to printf,
then main.o contains a use of the external symbol
_printf. (Sun's C compiler constructs an external symbol
name by prefixing the C identifier with an underscore; IBM's C
compiler constructs the name by prefixing a period. We will assume
underscore here.)
Any function or global variable that is not declared static
is automatically a definition of an external symbol. E.g., the
main function defines a symbol named _main.]
Uses of external symbols must be matched up against definitions
of those symbols.
At any point in the loading process, there are likely to be many
unresolved external symbols -- symbols which have been used
but have not yet been defined in any `.o' file.
repeat for X := each unresolved reference do look up X in the archive file's symbol table if X is found then extract the `.o' file from the archive file that defines symbol X, and add this new `.o' file to the program being created until no more symbols can be resolved.Note that extracting a `.o' file from the archive and adding it to the program may create new unresolved references. (E.g., the scanf library function may contain a call to the atoi library function, and these functions may be defined in different `.o' files in the archive.) That's why the process must be repeated until no more symbols can be resolved.
ld -o myprog /usr/lib/crt0.o /usr/lib/libc.a main.ois likely to produce error messages similar to:
ld: Error: undefined symbol: _printfbecause when libc.a was processed, there were no unresolved references. After main.o is processed, ld does not return to the libc.a file again.
ld -o myprog /usr/lib/crt0.o main.o /usr/lib/libc.ais equivalent to
ld -o myprog /usr/lib/crt0.o main.o -lcI.e., the notation ``-lXXX'' is equivalent to ``/usr/lib/libXXX.a'' where XXX represents any sequence of characters.
cc -o myprog main.c subr.cis exactly equivalent to the three commands:
cc -c main.c cc -c subr.c ld -o myprog /usr/lib/crt0.o main.o subr.o -lc
ar -v -t /usr/lib/libc.aThe ``-t'' option means table of contents; the ``-v'' option means verbose (in this case, we get a long listing of the `.o' files, much like executing `ls -l').
ar -v -x /usr/lib/libc.a strcat.o strcpy.oThe ``-x'' flag means extract.
ar -v -p /usr/lib/libc.a strcat.o > mystrcat.oThe `-p' flag means pipe because the extracted file is piped to standard output.
ar -v -q libfoo.a one.o two.o three.owhere
cc -o myprog main.c libfoo.a(The cc command will add libfoo.a to the argument list for ld.)
ar -v -r libfoo.a two.o four.o
ar -v -x /usr/lib/libc.a shr.o nm -g shr.oThe nm command can produce lots of output; the ``-g'' flag restricts the output to just global (external) symbols.