0%

使用 libtool 编译静态库

libtool

iperf3_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
    $(LIBTOOLFLAGS) --mode=link $(CCLD) $(iperf3_CFLAGS) $(CFLAGS) \
    $(iperf3_LDFLAGS) $(LDFLAGS) -o $@
LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
    $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \
    $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
    $(AM_CFLAGS) $(CFLAGS)

模式为 libtool --tag=CC xxx --mode=xx $(CC),在 $(CC) 后添加 Flag

iperf3_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
    $(LIBTOOLFLAGS) --mode=link $(CCLD) -all-static $(iperf3_CFLAGS) $(CFLAGS) \
    $(iperf3_LDFLAGS) $(LDFLAGS) -o $@
LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \
    $(LIBTOOLFLAGS) --mode=compile $(CC) -static $(DEFS) \
    $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \
    $(AM_CFLAGS) $(CFLAGS)

example

libtool looks at the parameters of gcc, so you should have something like below

$ cat Makefile
all: libone libtwo
        rm *.o
        @libtool --mode=link gcc -all-static -o libcombo.a libone.a libtwo.a

libone: one.c
        @libtool --mode=compile gcc -c one.c -o one.lo
        @libtool --mode=link gcc -static -o libone.a one.lo

libtwo: two.c
        @libtool --mode=compile gcc -c two.c -o two.lo
        @libtool --mode=link gcc -static -o libtwo.a two.lo

Ref

  1. How to use libtool to create a static library from a bunch of static libraries
  2. 3.2 Linking libraries
  3. 4.2 Link mode
  4. Building all static