Libraries in cc

coolbean nieters at phobos.crd.ge.com
Thu Oct 18 02:55:08 AEST 1990



;
;    Is it just me, or is the fact that "cc -lm mmnn.c" doesn't work but
; "cc mmnn.c -lm" does extremely non-intuitive?  [....]
;

well, yes and no.  yes if you view a library as a simple .o object
file, no if you view a library as it's truly meant to be.  when you
compile an executable and include a library file, _only_ those entries
in the library that are needed (ie. referenced) are extracted and included
in the output.  (if you don't believe this, write a one line program
that calls log2() from the math library "-lm," for example and, using
nm(1), search the executable's symbol table for some other math function
such as "pow".  it shouldn't be in there, but "log2.o" should be.)
if this functionality wasn't the case, then the entire library would
be pulled in even though it contains unused functions and executable
size would be huge.
	   now most compilers deal with files in the order
they are encountered on the command line.  when a library archive file
occurs, it is searched exactly once and only those archive entries
which match an unresolved external referece are extracted and concatenated
to the output.  
	so, getting back to your example "cc -lm mmnn.c"  ... the compiler
gets "-lm" first, searches it and finds no archive entries corresponding
to unresolved external references.  therefore no entries are pulled out
and the next file - mmnn.c - is dealt with.  references in this file to
entries in the math library can't be resolved since the compiler has
"forgotten" about the math library itself.  hence, the command line
must be "cc mmnn.c -lm".
	the only way i could think of that a compiler could deal with
the first (non-working) example, is to either "save" a pointer/descriptor
to the library file and, after all "normal" files have been handled, 
search the library to resolve any unresolved external references.

hope this helps.

--ed
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ed Nieters		     INTERnet: nieters at crd.ge.com
GE Corporate R&D	     UUCPnet:  uunet!crd.ge.com!nieters	       
Schenectady, NY 12301	     BELLnet:  (518) 387-5187



More information about the Comp.unix.questions mailing list