public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* problem with order of -lmylibrary flags.
@ 2012-05-20 23:16 Brendan Miller
  2012-05-21  4:18 ` Ian Lance Taylor
  2012-05-21 13:34 ` Kevin P. Fleming
  0 siblings, 2 replies; 3+ messages in thread
From: Brendan Miller @ 2012-05-20 23:16 UTC (permalink / raw)
  To: gcc-help

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.

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?

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?

Thanks,
Brendan Miller

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: problem with order of -lmylibrary flags.
  2012-05-20 23:16 problem with order of -lmylibrary flags Brendan Miller
@ 2012-05-21  4:18 ` Ian Lance Taylor
  2012-05-21 13:34 ` Kevin P. Fleming
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2012-05-21  4:18 UTC (permalink / raw)
  To: Brendan Miller; +Cc: gcc-help

Brendan Miller <catphive@catphive.net> writes:

> 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.
>
> 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?

That is how the linker works.  The placement of -l options is
significant.  A -l option only resolves undefined symbols in objects
seen before the -l option.


> 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?

The LDLIBS variables, which in most Makefiles is simply called LIBS.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: problem with order of -lmylibrary flags.
  2012-05-20 23:16 problem with order of -lmylibrary flags Brendan Miller
  2012-05-21  4:18 ` Ian Lance Taylor
@ 2012-05-21 13:34 ` Kevin P. Fleming
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin P. Fleming @ 2012-05-21 13:34 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-21 13:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-20 23:16 problem with order of -lmylibrary flags Brendan Miller
2012-05-21  4:18 ` Ian Lance Taylor
2012-05-21 13:34 ` Kevin P. Fleming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).