problem with order of -lmylibrary flags.

Kevin P. Fleming kpfleming@digium.com
Mon May 21 13:34:00 GMT 2012


On 05/20/2012 06:16 PM, Brendan Miller wrote:
> I've found kind of a weird issue where if I indicate I want to link a
> library with the -l flag too early on the command line, I get linker
> errors.
>
> Here's an example:
> g++ -lml -lcvaux -lhighgui -lcv -lcxcore   -std=c++0x -Werror -Wall -g
> -MMD -MP -Iinclude  -o objrec main.cpp
>
> Here I'm linking a bunch of libraries, into a binary called objerec.
> The source file is at the end. This command line produces linker
> errors as if the libraries had not been linked properly.

No, it produces linker errors because symbols referenced from main.cpp 
could not be found. The libraries are 'linked properly', you aren't 
linking the libraries as part of compiling and linking this program.

>
> However in this example everything is fine:
> g++  -std=c++0x -Werror -Wall -g -MMD -MP -Iinclude  -o objrec
> main.cpp -lml -lcvaux -lhighgui -lcv -lcxcore
>
> The only difference is that I place the -l flags after the source file
> (main.cpp) that I'm building.
>
> Why does the second example compile while the first gets linker errors?

They both compile, only the second links. This is true because the 
linker searches for symbols in libraries listed *after* the 
object/library that referenced that symbol.

Here is one (of many) web pages documenting this behavior:

http://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_18.html

>
> A more general question: in a make file is it better to list libraries
> you want to link in the LDFLAGS variable, or in the LDLIBS variable?

There is no such thing as a 'generic' Makefile. A Makefile defines its 
rules for compilation, linking and other aspects, and if it uses 
variables to hold libraries to be linked, then you need to use those 
variables as it intends. In general, though, LDLIBS is usually the place 
that libraries would be listed (thus the 'LIBS' in the variable name).

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
Jabber: kfleming@digium.com | SIP: kpfleming@digium.com | Skype: kpfleming
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at www.digium.com & www.asterisk.org



More information about the Gcc-help mailing list