lib

Leo de Wit leo at philmds.UUCP
Wed Jun 22 07:25:02 AEST 1988


In article <10566 at agate.BERKELEY.EDU> arnold2 at violet.berkeley.edu (mchawi) writes:
>intro:
>  linking together lots of utility-type files along with the main driver
>  produces a fairly large (500K+) program, e.g.,
>	"cc main.c stringsubs.o sort.o timesubs.o ttystuff.o"
>  each 'utility' file has many functions (e.g., 'stringsubs.c' has
>  50 functions). however, the program might use only a few of those.
>wanted:
>  find a way to link into the program only those functions that are used,
>  in order to minimize program size (another case of VAX->ibm-pc porting).
>vision:
>  have a linker that can extract the code (probably would have to include
>  all the data space for each file) into the executable file. perhaps you
>  can link from a library you create first? 
>alternatives:
>	1) break up the utility files. the most obvious solution.
>	disadvantages: lots of little files, e.g.
>	"cc main.c string1.o string2.o string3.o sort.o ..."; you
>	have to remember what functions are where. not elegant.
>
>if i were to use only the functions i needed, i could make a 1 meg program
>down to probably 250K. any ideas, system software engineers?

Since no one yet replied, I will lay my egg 8-).

You have indicated the solution yourself already: create a library that
contains all the objects (except main probably). If space is critical,
it is also important to keep the utility files, and hence the object
modules small. I'm pretty sure that most linkers can only extract all
functions from a module that is in a library (or you have a clever
linker). So I would suggest the following recipe:

1) Break up the utility files into small coherent pieces (breaking the egg)
2) Compile each object (add salt, milk and beat it up).
3) Put it into the library, using ar on Un*x (pour it into the frying pan).
4) Run ranlib on the library if you have a Un*x system to create a
random access library which speeds up linking (bake it on each side).
5) Link your compiled main with this library and any other library you
need (add ham and cheese and tomatoes and ... and ...).
6) Voila, here's your omelet! ;-)

Note that you don't have to worry about your functions, the library has
them (could even get a contents). What is your problem with little
files?  I think they are much nicer! You can better see what's in them,
they are faster edited, faster compiled. A good idea is to keep all
operations on a object in one file, so you can keep the locally needed
functions static.  The exported functions, variables, constants and
types (i.e. those used by other modules) you should declare in a header
file.
In this way you have a separation between the definition (header) part
and the implementation (C-source module) part. 



More information about the Comp.lang.c mailing list