Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between -I and -L in makefile?

What is the usage of the -I and -L flags in a makefile?

like image 823
MainID Avatar asked Feb 06 '09 06:02

MainID


People also ask

What does ?= Mean in 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"

What is in a Makefile?

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.


1 Answers

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.

like image 123
Nathan Fellman Avatar answered Oct 12 '22 14:10

Nathan Fellman