46

This is strange because I was able to get the error below to go away by removing the reference to libm.

gcc -o example example.o -Wl -L/home/kensey/cdev/lib -L/usr/lib/x86_64-linux-gnu   -lmysqlclient -lpthread -lz -L/usr/lib/x86_64-linux-gnu -lm -lrt -ldl -lcdev -L/home/kensey/www.tools/gplot-lib -lgplot -L/home/kensey/www.tools/gd1_3ret -lgd -lxml2 -lcurl
/usr/bin/ld: /home/kensey/www.tools/gplot-lib/libgplot.a(set.o): undefined reference to symbol 'floor@@GLIBC_2.2.5'
/usr/bin/ld: note: 'floor@@GLIBC_2.2.5' is defined in DSO /usr/lib/x86_64-linux-gnu/libm.so so try adding it to the linker command line
/usr/lib/x86_64-linux-gnu/libm.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status

So, if I remove the -lm part of the command, I do not get the error. However, I wonder if anyone knows as to why removing a reference to a library that is needed would fix this. How does the linker know which library to look in? Also - is there a way to query a built executable and say 'which library did you resolve the reference to 'floor'? obviously, there is something going on that I don't understand, and that bothers me...

1
  • 1
    the -Wl option is normally followed by a comma (to pass the text after the comma as an option to the linker), what do you intend to do with it ? Commented Jun 3, 2013 at 19:58

9 Answers 9

84

The explanation to what's happening is very simple:

  1. Your libgplot.a depends on libm.so, yet the order of -lm and -lgplot on the link line is wrong. The order of libraries on the link line does matter. In general, system libraries (-lpthread, -lm, -lrt, -ldl) should follow everything else on the link line.

  2. When you remove -lm from the link line, libm.so.6 is still pulled into the link by some other library that appears later on the link line (libgd, libxml2 or libcurl) because that library depends on libm.so.6. But now libm.so.6 is in correct place on the link line, and so everything works.

if I put -lm at the end of the link command, listing it as the last library, I do not get the error.

That confirms above explanation.

1
9

I've solved the same problem with export LDFLAGS="$LDFLAGS -lm"

3
  • Can you explain what this does? Commented Sep 8, 2015 at 18:35
  • 2
    -lm for linking against the standard C math library
    – dmnc
    Commented Oct 30, 2015 at 13:25
  • My problem wasn't exactly the original question, but adding -lm in the makefile at the end of my LDFLAGS definition worked. Thanks. Commented Mar 11, 2017 at 20:17
6

Perhaps, your library search paths (/usr/local/lib/ or /usr/lib/, ...) do not contain 64bit libm so gcc cannot locate it if you specify with l flag. If you only specify only the directory it looks like it can find the right one. So you can try:

LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu

and use -lm

1
  • ok I played around some more, and if I put -lm at the end of the link command, listing it as the last library, I do not get the error. The theory of a non 64bit libm might still be the case, as perhaps it can 'find the right one' before it gets to -lm at the end of the command, so the -lm is essentially ignored. fyi - I queried the libm via 'ar -t' and it listed the contents of the library ok. so that would imply its 64 bit/searchable.
    – Don Wool
    Commented Mar 29, 2012 at 23:48
3

Hard to tell. Because there are custom library directories in the command line it's conceivable that -lm links an incompatible alternative version. Without -lm the linker could pull in another version of it because it's needed by one of the libraries you link.

To make sure strace both invocations and see where libm.so is coming from in both cases.

BTW, -Wl switch seems to do nothing and -L/usr/lib/x86_64-linux-gnu is mentioned twice.

2
  • open("/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0pU\0\0\0\0\0\0"..., 832) = 832 fstat(3, {st_mode=S_IFREG|0644, st_size=1022320, ...}) = 0
    – Don Wool
    Commented Mar 30, 2012 at 0:13
  • it turned out to be the same for both.. <br>kensey@kensey:~/cdev$ strace ./example 2>&1 | grep libm open("/usr/lib/x86_64-linux-gnu/libmysqlclient.so.18", O_RDONLY|O_CLOEXEC) = 3 open("/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3 <br>so, I am not sure why this was a problem, but luckily it went away. I guess the lesson is : make sure libraries come after other libraries that may reference them. there is probably a way with strace to do more investigation, but I am new to that tool. thx for the help!
    – Don Wool
    Commented Mar 31, 2012 at 6:10
2

Just to add to the list of answers, http://fedoraproject.org/wiki/UnderstandingDSOLinkChange It is informative. It isn't relevant to the question asked above, but, the explanation relates to the error message /usr/bin/ld: note: 'some_reference' is defined in DSO some.so so try adding it to the linker command line

1
  • in fact, it seems to me this is a change of behaviour with respect to earlier versions of the GNU toolchain. If I remember correctly, was the order of the libraries given irrelevant in the past for gcc and it was smart enough to find the symbols. With xlC on AIX, the order was important if I remember correctly also in the past... Commented Jun 3, 2013 at 19:52
1

One explanation could be:

It's possibly there is a weakly linked function foo defined outside of libm that is replaced by a strongly linked version of foo defined inside libm, and it is this strongly linked version that calls the undefined function.

This would explain how adding a library can cause an undefined function error.

2
  • Note, that the symbol is versioned. Does it still apply? Commented Mar 29, 2012 at 22:54
  • @MaximYegorushkin: Not sure sorry. I think the version of the undefined symbol is orthogonal to my possible diagnosis. Commented Mar 29, 2012 at 22:57
1

I just ran into a similar problem; I remember that the order of the libraries did not matter (at least not in the cases I worked with) in the past for gcc. In this question here somebody noticed that the behaviour seems to have changed between 4.4 and 4.5 .

In my case, I got rid of the error message by doing the linking at:

 g++ -Wl,--copy-dt-needed-entries [options] [libraries] [object files] -o executable-file
1
  • The GCC version is irrelevant, you're talking about a linker change not a compiler one. Commented Jun 3, 2013 at 20:06
0

I faced the similar issue because I had manually updated the dev toolchain on my centOS machine to solve a VScode Remote dependency and was linking C++ library with c code.

In my case, I solved this by adding in the Makefile: LDFLAG=-Wl,--copy-dt-needed-entries

I also pointed my gcc to the version I wanted (After updating toolchain, gcc pointed to the toolchain : /opt/rh/devtoolset-2/root/usr/bin/gcc)

CC=\usr\bin\gcc which is (gcc version 4.4.7)

-1

Use this:

administrator@administrator-Veriton-M200-H81:~/ishan$ gcc polyscanline1.cpp -lglut -lGLU -lGL -lm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.