[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting 'undefined reference' errors during linking but I think I in

From: Rusty Ballinger
Subject: Re: Getting 'undefined reference' errors during linking but I think I included all libraries?!?
Date: Fri, 2 May 2003 01:08:29 -0700

> Did you know that when you link all the stuff to make
> the executable that the *order matters*?!?!?

(A more serious & deep-seated problem, I think, is that order matters
when running individual commands.  For years I have been waiting for
this sort of thing to get fixed:

  $ rm -f foo
  #  wait, I need that stuff!  better make a backup.
  $ cp foo foo.bak
  cp: cannot stat `foo': No such file or directory

"You bastards!")

> gcc -o myprogram myprogram.o func1.o func2.o
>       -L $(srcdir)/libs -lsomearchive -lm -lX11
>        -lpthread -L somepath ...etc.
> The -L guys *must* be *after* the object files!!!!
> The whole reason for my problem was that I put the
> -L stuff in my name_LDFLAGS line instead of
> name_LDADD!!!

I don't think that's true:

  $ g++ -o foo foo.o -lsomeLib
  /tmp/cc2eBnZJ.o: In function `main':
  /tmp/cc2eBnZJ.o(.text+0x1e): undefined reference to [...]
  collect2: ld returned 1 exit status

  $ g++ -L/usr/tmp/somelibdir -o foo foo.o -lsomeLib

That's with gcc 3.2, but I think any compiler will be the same.  I
think all that matters is that the -L precedes the -l.

> Not only that but if some libraries depend on other ones
> then you must be careful of the /order/ you specify all the -l guys in!!!

Yes, "but", your compiler's man page or other documentation will tell
you that.  (& although I think putting all your -L's in LDFLAGS is
probably fine, you can get finer control, if you need it, by putting
"-L/path/to/foolib -lfoo -L/path/to/barlib -lbar" etc. in LDADD.
Also, automake's info page does say it's OK to put -L's in LDADD.)

> I kept modifying the gcc link line and finally got one
> that WORKED!!! The trick now is getting to reflect
> that.

You "should" be able to just put your -l's in the right order in LDADD
or foo_LDADD; isn't automake leaving them alone?

(also, in case it's not clear, although -L's and -l's can be intermixed,
the -L which says where to find a library needs to precede the -l which
gives the name of that library.)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]