What is the usage of the -I and -L flags in a makefile?
?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR) Would print "foo"
Makefiles contain five kinds of things: explicit rules , implicit rules , variable definitions , directives , and comments . Rules, variables, and directives are described at length in later chapters. An explicit rule says when and how to remake one or more files, called the rule's targets.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With