Keith M
Keith M

Reputation: 893

Construct LDFLAGS from list of full library paths in Makefile

I want to construct my linker flags LDFLAGS from a list of libraries with their full paths. Here's where/why I'm stuck. (The library names are Linux style, but that shouldn't really matter for how to solve this, I think.)

FULL_LIBS = \
    ./path1/libone.a \
    ../../path2/libtwo.a
# etc.    

LIB_DIRS = $(dir $(FULL_LIBS))
LIB_NAMES = $(basename $(notdir $(FULL_LIBS)))
LIB_NAMES_TRIMMED = # zzz trim leading "lib" somehow??? This is what I don't know how to do, or at least the best way

LDFLAGS += \
    $(addprefix -L, $(LIB_DIRS)) \
    $(addprefix -l, $(LIB_NAMES_TRIMMED))

The desired result, if it's not obvious, should be like

LDFLAGS += -L./path1 -L../../path2 -lone -ltwo

Not sure if I NEED regular expressions (or sed/awk/grep), so I'll withhold that tag () for now.

Upvotes: 0

Views: 297

Answers (1)

FrancoisB
FrancoisB

Reputation: 321

You can use the text processing capabilities of make (https://www.gnu.org/software/make/manual/html_node/Text-Functions.html#Text-Functions) :

LIB_NAMES_TRIMMED = $(subst lib,,$(LIB_NAMES))

Although this could fail if the string "lib" appears elsewhere in the library names. To avoid that we can use 2 steps:

LIB_NAMES_TRIMMED = $(basename $(notdir $(subst /lib,/,$(FULL_LIBS))))

As explained by Matt, it's even simpler with patsubst:

LIB_NAMES_TRIMMED = $(patsubst lib%,%,$(LIB_NAMES))

Upvotes: 1

Related Questions