c math linker problems on Ubuntu 11.10

Hachi picture Hachi · Oct 19, 2011 · Viewed 31.1k times · Source

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

Answer

user786653 picture user786653 · Oct 19, 2011

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