[comp.unix.questions] Help making object file archives

wyngaard@wayne (Peter Wyngaard) (06/12/91)

I have a library of routines spread out over many directories and I am trying
to write the (gnu)makefiles for them.  I don't like the built-in implicit
rule for making an archive member from an object file because it does an
'ar' for each object file.  This is VERY slow.

I have played around a little and it seems as though there should be some
way to trick gnumake into letting me do one 'ar' after all the '.c' files
in a directory have been compiled.

Here is about as close as I've come:

LIB = anylib.a
#
# Get rid of gnumake's implicit rule for making archive members and replace
# it with my own
#
(%) : %

(%.o) : %.c
        $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $% $<
#
# Silly macros for extracting the object file name from a list of archive
# elements
#
lpar = (
rpar = )
sp = $(empty) $(empty)
object_files = $(filter %.o,$(subst $(lpar),$(sp),$(subst $(rpar),$(sp),$?)))

$(LIB) : $(LIB)(file1.o) $(LIB)(file2.o) $(LIB)(file3.o) $(LIB)(file4.o)
        $(AR) $(ARFLAGS) $(LIB) $(object_files)
        $(RM) $(object_files)
        $(RANLIB) $(LIB)

This doesn't work, however, because the object files aren't put into the  
archive by the '(%.o) : %.c' rule hence the '$(AR) - $(RM) - $(RANLIB)'  
sequence is never executed.

I know this would be easy if I just kept copies of the object files in the
source directories, but this takes up too much disk space.

If you have any suggestions, please send them to me via email.

Thanks in advance,

Peter
wyngaard@ircam.fr

darcy@druid.uucp (D'Arcy J.M. Cain) (06/13/91)

In article <1991Jun12.121206.26970@ircam.fr> Peter Wyngaard writes:
>to write the (gnu)makefiles for them.  I don't like the built-in implicit
>rule for making an archive member from an object file because it does an
>'ar' for each object file.  This is VERY slow.
[stuff deleted]

Quite simple actually.  Try this:

-------------------------------------------------------
UNIX_L=/usr/lib/libmine.a
ALL_OBJECTS =	parse nx_wk_dy strmem mktime [etc ...]

$(UNIX_L):	$(foreach o, $(ALL_OBJECTS), $o.o)
	ar r $(UNIX_L) $?
--------------------------------------------------------

The only problem is that you are left with .o files.  This is what
the standard method avoids.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |