This is the mail archive of the
automake@gnu.org
mailing list for the automake project.
Re: Getting 'undefined reference' errors during linking but I thinkI included all libraries?!?
- From: <cs at gnumatica dot com>
- To: rusty at sgi dot com
- Cc: rusty at rlyeh dot corp dot sgi dot com
- Cc: automake at gnu dot org
- Date: Fri, 2 May 2003 08:55:33 -0700 (MST)
- Subject: Re: Getting 'undefined reference' errors during linking but I thinkI included all libraries?!?
- References: <10305020108.ZM243025@rlyeh.corp.sgi.com>
Thanks. I'm /very/ happy this got cleared up.
Chris
-------- Original Message --------
Subject: Re: Getting 'undefined reference' errors during linking but I
think I included all libraries?!?
From: "Rusty Ballinger" <rusty@rlyeh.corp.sgi.com>
Date: Fri, May 2, 2003 1:08 am
To: cs@gnumatica.com
> 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 Makefile.am 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.)
--Rusty