0

I got the same issue than How to create one static library from several others static libraries in C on Linux?

The answer worked for me when I did manually, but how to automatize this with a Makefile ? My current rule is:

$(STATIC_LIB_TARGET): $(LIB_OBJS) $(THIRDPART_LIBS)
    $(Q)cd $(BUILD) && $(AR) $(ARFLAGS) $@ $(LIB_OBJS) && ranlib $@

$(STATIC_LIB_TARGET) is my static lib I want to create, $(LIB_OBJS) contains my project .o files inside my output folder $(BUILD) and $(THIRDPART_LIBS) contains the list of external .a that I have to ar -x. My issue is that ar -x shall be called for each static lib and cannot be grouped and the list of extracted .o files shall be got into Makefile variable.

I could use foreach over $(THIRDPART_LIBS) or call shell script, but this seems complicated. I think a more elegant solution exists.

Thanks in advance.

1 Answers1

0

Ok, since I did not have an answer. I've finally done by calling external bash script. Ugly but seems working for me. Hope to have a proper way of doing it.

Makefile:

$(STATIC_LIB_TARGET): $(LIB_OBJS) ...
ifneq ($(THIRDPART_LIBS),)
    ./$(M)/alib.sh $(BUILD) $(THIRDPART_LIBS)
    cd $(BUILD) && $(AR) $(ARFLAGS) $@ $(LIB_OBJS) $(THIRDPART_OBJS) _lib*/*.o && ranlib $@
else
    cd $(BUILD) && $(AR) $(ARFLAGS) $@ $(LIB_OBJS) $(THIRDPART_OBJS) && ranlib $@
endif

Bash:

#!/bin/bash
BUILD=$1
shift

for LIB in $*;
do
   FOLDER=$BUILD/_`basename $LIB .a`
   rm -fr $FOLDER 2> /dev/null
   mkdir -p $FOLDER
   (cd $FOLDER; ar -x $LIB)
done