views:

988

answers:

6

Where can I find the usage of these stuff?

Is there some books that cover them?

+3  A: 

man pages ! Gnu Make Manual too

Scott Evernden
I was tempted to vote this down as not answering the question until I saw that the OP also asked for books with answers. I *do* think that you should add a link to the gcc manpage to provide a fuller answer.
Nathan Fellman
+12  A: 

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.

Nathan Fellman
Voted up since I prefer self-contained answers without links (as long as they're right).
paxdiablo
Rather than repeat a nice answer, I added a little context. +1
dmckee
A: 

This is a more verbose reference. See here for options.

kgiannakakis
A: 

For books:

Check out this classic one:

GNU Make from O'Reily

Roalt
This book is OK, I guess. I have both this and the previous edition that didn't have GNU in the title, but I can't say it's been a huge help for me when dealing with complex Make issues. Basic Make, perhaps it's a fine book.
Mitch Haile
+1  A: 

One thing to note is that these are the options passed to the compiler/linker. So you should be looking at the compiler man pages/documentation to know their role.

sateesh
+2  A: 

To really grok a makefile, you need to also have a good understanding of the command lines for all of the components of your project's toolchain. Options like -I and -L are not understood by make itself. Rather, make is attempting to create a command line that will execute a tool to transform a prerequisite file into a target file.

Often, that is a C or C++ source file being compiled to an object file, and eventually linked to get an executable file.

In that case, you need to see the manual for your compiler, and especially the bits related to the command line options it understands.

All that said in generic terms, those specific options are pretty standard among compilers and linkers. -I adds a directory to the list of places searched by the compiler for a file named on a #include line, and -L adds a directory to the list of places searched by the linker for a library named with the -l option.

The bottom line is that the "language" of a makefile is a combination of the syntax of the makefile itself, your shell as known to make (usually /bin/sh or something similar), common shell commands (such as rm, cp, install, etc.), and the commands specific to your compiler and linker (e.g. typing gcc -v --help at your shell prompt will give you a nearly complete (and extremely long) list of the options understood by gcc as one starting point).

RBerteig
good explanation!
Nathan Fellman