Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cannot use math.h - undefined reference to `sqrt'

15,333 views
Skip to first unread message

Warren Tang

unread,
Apr 15, 2008, 10:28:56 AM4/15/08
to
When I try to compile the following program, the gcc complains that:

main.c.text+0x35): undefined reference to `sqrt'
collect2: ld returned 1 exit status

#include <stdio.h>
#include <math.h>

int main(void)
{

double x = 10;

double y;

y = sqrt(x);

return 0;

}

I've checked that the math.h file is indeed in the following folder:
/usr/include

But why can't gcc find it? How can I use math.h?

Thanks for any help!

Günther Jehle

unread,
Apr 15, 2008, 10:36:35 AM4/15/08
to
> But why can't gcc find it? How can I use math.h?

including math.h is not the problem. gcc is just missing the code for the
math functions. You can achieve this by adding "-lm" to your gcc command
line. Now, the math library is added.

gcc main.c -lm

hth
Guenther


Warren Tang

unread,
Apr 15, 2008, 10:41:57 AM4/15/08
to
By searching the internet I found I need to use the "-lm" option to
include "math library". But I don't know why I have to do this.. Isn't
math.h just an ordinary header file just as stdio.h? What makes the
difference?

Warren Tang

unread,
Apr 15, 2008, 10:42:31 AM4/15/08
to
Thank you!

Günther Jehle

unread,
Apr 15, 2008, 12:23:22 PM4/15/08
to
> By searching the internet I found I need to use the "-lm" option to
> include "math library". But I don't know why I have to do this.. Isn't
> math.h just an ordinary header file just as stdio.h? What makes the
> difference?

the header files only have the declarations of the functions, not the source
itself. The functions in stdio.h are in the glibc library, which is linked
by default. The math functions are not defined in glibc, they are in the
math library. And to link this library you have to "-lm".

hth
Guenther


Nikos Chantziaras

unread,
Apr 15, 2008, 4:29:49 PM4/15/08
to
Warren Tang wrote:
> By searching the internet I found I need to use the "-lm" option to
> include "math library". But I don't know why I have to do this.. Isn't
> math.h just an ordinary header file just as stdio.h? What makes the
> difference?

The compiler automatically links against libc (the C library) so that
you don't have to specify "-lc" every time. The math functions are in
libm though, and since the compiler doesn't link against it
automatically, you need "-lm".

sri.gem...@gmail.com

unread,
Dec 21, 2012, 7:28:13 AM12/21/12
to
e1 after using '-lm' for math.h progs i m getting

/tmp/ccrKnLP1.o: In function `main':
triangle.c:(.text+0xfd): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

i donno wt to do

b...@coolfone.comze.com

unread,
Dec 21, 2012, 10:18:23 AM12/21/12
to
What OS are you on?

I was able to compile/build this with just "gcc test.c" on Mac Os Mountain Lion:

valdez...@gmail.com

unread,
Aug 22, 2014, 7:50:01 AM8/22/14
to
thank you ;)

k.rak...@gmail.com

unread,
Aug 26, 2014, 6:44:05 AM8/26/14
to
when compliling gcc filename.c -lm
0 new messages