Results 1 to 10 of 10
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
What's the problem? I am using ...
- 04-15-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 16
undefined reference to `sqrt'
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
What's the problem? I am using Fedora 8 by the way.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 10;
double y;
y = sqrt(x);
return 0;
}
Thanks for any help!
- 04-15-2008 #2Just Joined!
- Join Date
- Mar 2008
- Posts
- 16
I've checked that the math.h file is in the following folder:
/usr/include
But why cann't gcc find it?
- 04-15-2008 #3Just Joined!
- Join Date
- Mar 2008
- Posts
- 16
I need to add the "-lm" option for the gcc
- 04-15-2008 #4
-lm option
For some reason I have to do this as well(add the -lm option to the gcc compile line)
- 04-15-2008 #5
Correct. You need to add "-lm" to gcc.
Most of the C Standard Library exists in a library called libc.so. Math functions, however, exist in libm.so. gcc will automatically link to libc: any other linking must be specified. To tell it what library to link to, you remove the '.so' from the end and the 'lib' from the beginning. Therefore, "-lm" means "link against libm.so".
I hope this makes sense.DISTRO=Arch
Registered Linux User #388732
- 04-15-2008 #6
Math library
Isn't the math header supposed to be part of the standard library, according to the ansi standard?
- 04-16-2008 #7
Both the header and the library are considered a part of the C Standard Library. However, this is not a compiletime error ("I don't know what sqrt is"). This is a linktime error ("I can't find the precompiled sqrt function").
I admittedly don't know the history behind this decision, or what the ANSI standard says about it, but that's the way it is. If you find out why it is this way, I'd be interested.DISTRO=Arch
Registered Linux User #388732
- 04-16-2008 #8Just Joined!
- Join Date
- Mar 2008
- Posts
- 16
- 03-19-2009 #9Just Joined!
- Join Date
- Feb 2008
- Posts
- 1
I have some issue about anything similar to '-lm' ?
So, -lm for math.h,but if these is anything similar to math.h,where can I know to add it for gcc
- 06-27-2009 #10Just Joined!
- Join Date
- Sep 2008
- Location
- Silicon Forest, Oregon
- Posts
- 4
Old info, but still useful. I just ran into the same problem. Adding m to the libraries in Eclipse solved the problem. Thanks.



