Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

g++ - gcc: Difference between -L and -l option AND how to provide complete path to a library

I am new to makefile stuff; so, please accept my apology if my questions are trivials.

Question 1: What is the difference between -L and -l option.

Question 2: How do you provide complete path to some library? For instance, "libeng" and "libmx", mentioned in following makefile,are located at {MATLABROOT}/bin/glnxa64

# root directory of MATLAB installation
MATLABROOT="/usr/local/MATLAB/R2011b"

all: engdemo

engdemo:
    g++ ${MATLABROOT}/extern/examples/eng_mat/engdemo.cpp -o engdemo 
        -I${MATLABROOT}/extern/include 
        -L${MATLABROOT}/extern/lib -llibeng -llibmx

clean:
    rm -f engdemo *.o

Update: Following makefile works:

# root directory of MATLAB installation
MATLABROOT="/usr/local/MATLAB/R2011b"

all: engdemo

engdemo:
    g++ ${MATLABROOT}/extern/examples/eng_mat/engdemo.cpp -o engdemo 
        -I${MATLABROOT}/extern/include 
        #-L${MATLABROOT}/extern/lib -llibeng -llibmx
    -L${MATLABROOT}/bin/glnxa64 -llibeng 
    -L${MATLABROOT}/bin/glnxa64 -llibmx

clean:
rm -f engdemo *.o

I found following link about linking libraries very useful: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is refering to gcc linker (or simply ld). Description can be found in in gcc's Options for Linking or ld's Command Line Options.

From the documentation you get

-larchive Add archive file archive to the list of files to link. ld will search its path-list for occurrences of libarchive.a for every archive specified.

-Lsearchdir Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts.

In your example you need to use -L to define the path where libeng and libmx libraries are located. Then use -l option to instruct ld to use these libraries.

Note that in the documentation is noted that:

ld will search its path-list for occurrences of libarchive.a for every archive specified.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...