Diego Herranz
Diego Herranz

Reputation: 2937

make's foreach and specifying libraries to link against (-l)

In a Makefile, I have a list of libraries I have to link against:

LIBS=var.a foo.a

I want to run the following command (simplified):

$(CC) main.c -lvar.a -lfoo.a

Now I use:

$(CC) main.c $(foreach lib,$(LIBS),-l$(lib))

It works, however it looks a bit wordy/cumbersome. Is there any better way to do it? Another approach?

Thanks!

Upvotes: 0

Views: 82

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

One possible improvement is to use the addprefix function.

$(addprefix -l,$(LIBS)

One could also use patsubst

$(patsubst %,-l%,$(LIBS))

Upvotes: 2

Related Questions