Dylan_Larkin
Dylan_Larkin

Reputation: 513

makefile explicitly linking multiple dynamic libraries

I'm trying to put together a simple makefile example like so:

FLAGS = -std=c++14
INC= -I/usr/local/include
LI = -L/usr/local/lib
LIB = /usr/local/lib/
LIBS =  $(LIB)libboost_filesystem-mt.a \
    $(LIB)libboost_filesystem-mt.dylib \
    $(LIB)libboost_filesystem.a \
    $(LIB)libboost_filesystem.dylib \
    $(LIB)libboost_system-mt.a \
    $(LIB)libboost_system-mt.dylib \
    $(LIB)libboost_system.a \
    $(LIB)libboost_system.dylib

default:
    g++ main.cpp $(FLAGS) $(INC) $(LI) $(LIBS) -o assemble
    ./assemble

clean:
    rm assemble

Is there a way to not have to prepend $(LIB) so many times? That's the only way I can get this to work right now (the above doesn't).

Upvotes: 0

Views: 342

Answers (1)

MadScientist
MadScientist

Reputation: 100781

If you want the linker to search the path you have to add libraries using the -l flag. So instead of adding libboost_system-mt.a to your link line, you have to add -lboost_system-mt to your link line. Then the linker will search the paths provided by -L.

I'm not sure about the dylib stuff; I don't do much with OS X.

In any event, if you're using GNU make you can do this:

LIBNAMES := filesystem-mt filesystem system-mt system

LIBS := $(foreach N,$(LIBNAMES),$(LIB)libboost_$N.a $(LIB)libboost_$N.dylib)

Upvotes: 1

Related Questions