21

Some strange error appeared after I upgraded my Ubuntu from (10.11, 11.04 i dont know) to 11.10.

I am getting an undefined reference to 'sqrt' while using math.h and linking with -lm

I'm compiling with gcc -Wall -Werror -g -Iinclude/ -lm lib/matrix.c src/analyse.c -o bin/analyse.o both source-files use and include math.h.

This code compiled without problems for and I didn't change much since the upgrade but now it won't work.

Do you have any suggestions what I can do, to find the error?

I'm sorry, if this question was asked before; there are so many posts on math linker errors and I didn't find a matching one

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Hachi
  • 3,237
  • 1
  • 21
  • 29
  • I do have the same problem in Ubuntu 11.10. I didn't have any problem before upgrading. In my case the problem comes from following command, Do you have any comments for me? gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -lm -L. -g -DASSERTS -I../src// -I../ -I../src//src -DDEBUG -lmems_internals – ARH Nov 08 '11 at 23:24

5 Answers5

27

The library that you are using needs to be placed after the files that use it when you are using it from the command line. So place -lm on after your C files on the command line.

Reference

bakoyaro
  • 2,550
  • 3
  • 36
  • 63
user786653
  • 29,780
  • 4
  • 43
  • 53
  • 1
    This is not accurate. Normally, the order does not matter when the library is shared. The problem only appears when --as-needed is either specified, or set as default. – Vladimir Prus Nov 16 '11 at 07:59
  • 3
    @VladimirPrus: You are both right :-). Recent Ubuntu versions (since 11.04/Natty Narwhal) set --as-needed as default. See https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition – sleske May 17 '12 at 13:48
  • @sleske erg.. that's a mistake now? quite a number of toolchans always put file list at the end of command (Qt for example) by design. That also creates even more issues with automake\cmake – Swift - Friday Pie May 06 '17 at 06:31
17

SOLVED, this is not the common missing -lm problem! I'm in the same situation after upgrade to (k)ubuntu 11.10!

$ whereis math.h
math: /usr/include/math.h

Makefile:
CC=gcc
CFLAGS=--std=c99 -g -pedantic -Wall -lm

uname:
Linux idefix 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

You really HAVE TO place the -lm swith after -o foo foo.c parameter

Output:
pidi@idefix:~/projekt1$ make
gcc -o b1 b1.c --std=c99 -g -pedantic -Wall -lm
pidi@idefix:~/projekt1$

So swap your flags in Makefile! GUYS. This is pretty new (and serious) BUG!

quido.speedy
  • 171
  • 3
10

This is a problem due to the default activation of the gcc flag --as-needed in the linker

More information here: http://www.gentoo.org/proj/en/qa/asneeded.xml

Simple fix (worked for me at least):

Add -Wl,--no-as-needed to the linker

Seldon_SL
  • 101
  • 2
  • Slight drawback of this approach is that the Math library is always included, also if the math library is not used. So don't use if you always include a lot of libraries independent if they're used or not. – Roalt Dec 07 '12 at 08:49
  • However, the linker is unable to find references when files are given in the wrong order also when linking with static libraries. Presumably, `ld --no-as-needed`, tagging all shared libraries anyway, lets `ld.so` work out the references. – Ale Oct 21 '21 at 10:58
4

I found the same problem after upgrading my Ubuntu to 11.10 version. I use Netbeans for developing and solved the problem by specifying the "Mathematics" standard library as it follows:

Right click on project, click on Properties, select "Linker" on menu, click on "Libraries" and then "Add Standard Library" choosing "Mathematics".

When compiling the '-lm' option is placed after all the other options and it works. Probably this gcc version follows a specific architecture and it expects the libraries at the end of the command compiling line.

Cheers!

D.

Diego
  • 41
  • 1
2
cc filename.c -lm

just try..........☻

laalto
  • 150,114
  • 66
  • 286
  • 303