[Bug 945927] Re: ld incorrectly gives 'undefined reference'
CF
freysoldt at mpie.de
Mon Mar 5 23:18:44 UTC 2012
Hi Matthias,
thanks for your patience. First of all, I just noticed a mistake in my
simple example linkage of libfoo - I forgot to link libfoo with libm at
creation time, which is crucial for this bug. The commands to use are
gcc -fPIC -c a.c
gcc -shared -fPIC a.o -o libfoo.so -lm
gcc -fPIC -c b.c
gcc -shared -fPIC b.o -o libbar.so
When I now type
gcc c.c -L . -lbar -lfoo -lm -Wl,--trace-symbol=sinh
I get
./libbar.so: reference to sinh
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/libm.so: definition of sinh
./libbar.so: undefined reference to `sinh'
collect2: ld gab 1 als Ende-Status zurück
(in case you are not German-speaking: collect2: ld returned exit code 1)
The same happens if I change the order into -lfoo -lbar -lm
What works, however, is to leave out -lfoo
gcc c.c -L . -lbar -lm -Wl,--trace-symbol=sinh
Now, according to my understanding of the linker, the presence of
superfluous -lfoo should not be able to break the link. Please correct
me, if I am wrong.
The point is that -lm gets very early into LIBS in the configure (which used to be very reasonable). If I use AC_CHECK_LIB (bar, ...) even with the extra-libs option), it will always test for
-lbar $LIBS extralibs
so I have no chance to squeeze the -lm between the tested -lbar and $(LIBS). And even if I would manage to, it is not sure whether libtool would not remove double -lm's when we do the real linking in the Makefile. When I said that this bug breaks AC_CHECK_LIBS, I meant exactly this: there is no easy way to check for multiple libs, without introducing non-standard dependencies on the libraries like "please test lbar only in the absence of lfoo, even if you may want to link them botht a later stage".
Christoph
--
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to binutils in Ubuntu.
https://bugs.launchpad.net/bugs/945927
Title:
ld incorrectly gives 'undefined reference'
Status in “binutils” package in Ubuntu:
Invalid
Bug description:
I had an unexpected link problem while trying to probe libraries in a
configure script.
I am using the latest update (2012-03-03 21:00 MEZ) of kubuntu 11.10, the binutils package is 2.21.53.20110810-0ubuntu5.1
My machine is a Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz, but I doubt that matters.
I was able to cook the problem down to the following.
Situation:
libfoo.so needs libm, and was created with -lm (ldd shows the dependency)
libbar.so needs libm, but was not created with -lm (no ldd dependency)
My program only needs libbar.so
Problem:
gcc prog.c -L. -lbar -lfoo -lm
yields an 'undefined reference to sinh'
If I run the link with -Wl,--trace-symbol=sinh, the linker actually reports that -lm contains sinh, but fails nevertheless.
What works is
gcc prog.c -L. -lbar -lm
gcc prog.c -L. -lbar -lm -lfoo
[The example of course cries for a simple workaround by reordering the
libs, but this is a mess when you try it in the configure. So just
take the lib ordering for granted (and this configure works on dozens
of other systems). I also know that libbar isn't up to the standards -
but, in real life, I won't change the way how other packages create
their shared libs.]
Here's some simple C files to reproduce the bug
--- a.c:
#include <math.h>
double foo (double x) { return exp(x); }
---- b.c:
#include <math.h>
double bar(double x) { return sinh(x); }
---- c.c:
double bar (double);
int main ()
{
double x = bar (1.);
}
---
gcc -fPIC -c a.c
gcc -shared -fPIC a.o -o libfoo.so
gcc -fPIC -c b.c
gcc -shared -fPIC b.o -o libbar.so
gcc c.c -L. -lbar -lfoo -lm
ProblemType: Bug
DistroRelease: Ubuntu 11.10
Package: binutils 2.21.53.20110810-0ubuntu5.1
ProcVersionSignature: Ubuntu 3.0.0-16.28-generic 3.0.17
Uname: Linux 3.0.0-16-generic x86_64
ApportVersion: 1.23-0ubuntu4
Architecture: amd64
Date: Sat Mar 3 23:08:39 2012
InstallationMedia: Kubuntu 10.04.1 LTS "Lucid Lynx" - Release amd64 (20100816.2)
ProcEnviron:
LANGUAGE=
PATH=(custom, user)
LANG=de_DE.UTF-8
SHELL=/bin/tcsh
SourcePackage: binutils
UpgradeStatus: Upgraded to oneiric on 2012-02-29 (3 days ago)
To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/binutils/+bug/945927/+subscriptions
More information about the foundations-bugs
mailing list