These are typically part of the linker command line, and are either supplied directly in a target action, or more commonly assigned to a make
variable that will be expanded to form link command. In that case:
-L
is the path to the directories containing the libraries. A search path for libraries.
-l
is the name of the library you want to link to.
For instance, if you want to link to the library ~/libs/libabc.a
you'd add:
-L$(HOME)/libs -labc
To take advantage of the default implicit rule for linking, add these flags to the variable LDFLAGS
, as in
LDFLAGS+=-L$(HOME)/libs -labc
It's a good habit to separate LDFLAGS
and LIBS
, for example
# LDFLAGS contains flags passed to the compiler for use during linking
LDFLAGS = -Wl,--hash-style=both
# LIBS contains libraries to link with
LIBS = -L$(HOME)/libs -labc
program: a.o b.o c.o
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@
# or if you really want to call ld directly,
# $(LD) $(LDFLAGS:-Wl,%=%) $^ $(LIBS) -o $@
Even if it may work otherwise, the -l...
directives are supposed to go after the objects that reference those symbols. Some optimizations (-Wl,--as-needed
is the most obvious) will fail if linking is done in the wrong order.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…