ARH
ARH

Reputation: 1395

Linking Math Library in GCC 4.6.1 (Ubuntu 11.10)

I find a problem in the linking process of my application. I did not have the same with gcc 4.5. It tries to link math library with the following command.

gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o -lm -L. -g -DASSERTS  -I../src// -I../ -I../src//src -DDEBUG -lmems_internals

and report following error massages:

undefined reference to `sqrt'

Any idea ?

Upvotes: 2

Views: 3171

Answers (4)

NickZoic
NickZoic

Reputation: 7835

I've had the same problem with gcc 4.6.1, even with only one library. This doesn't work:

$ gcc -lm eg.o -o eg
eg.o: In function `foo':
/home/nick/tmp/eg.c:5: undefined reference to `sqrt'
collect2: ld returned 1 exit status

But this does:

$ gcc eg.o -o eg -lm

I hit this because I was using "LDFLAGS=-lm" in my Makefile. Works fine if you use "LDLIBS=-lm" instead.

Upvotes: 4

nos
nos

Reputation: 229088

recent gcc/ld uses the --as-needed linker flag as default. Practically, that means libraries have to be specified in the reverse order of dependencies on the command line. If the mems_internals library needs the sqrt function your -lm after -lmems_internals.

gcc -Wall -Wno-unused -MD -o mems_seektest mems_seektest.o  -L. -g -DASSERTS  -I../src// -I../ -I../src//src -DDEBUG -lmems_internals -lm

Upvotes: 7

Jens Gustedt
Jens Gustedt

Reputation: 78903

You didn't tell us what -lmems_internals is, but maybe the unresolved symbol comes from there. The order of the -l options is generally important to the linker, you should always put system libraries last.

You can check where the unresolved symbol comes from by using something like

nm yourLibrary | grep sqrt

if there is a U in front of sqrt the symbol is undefined.

Upvotes: 3

alk
alk

Reputation: 70911

I'd say the linker is using the wrong libm.

Upvotes: 0

Related Questions