Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

math.h File seems incomplete

Status
Not open for further replies.

jayson22

Programmer
Jun 12, 2002
1
0
0
US
I am trying to use the sqrt() function that, according to all the help and tutorials I have read, should be included with the #include <math.h> call. This does not seem to be the case as I have tried to compile a bit of source code on two different machines and both of the have failed due to sqrt() being an undefined function. The code snipit looks like this:

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

char input[80];
float ...;
double distSqr, dist;

int main()
{

...

dist = sqrt(distSqr);

...

return(0);
}

My compile line looks like this:

gcc -g -Wall -ansi -pedantic -odist dist.c

And the error I get is:

/tmp/ccY4jY7J.o: In function `main':
/home/.../dist.c:19: undefined reference to `sqrt'
collect2: ld returned 1 exit status

I have browsed the math.h file but I see no reference to the sqrt() function, could someone please help me. I am going to loose it! :)

Thanks
-Jayson
 
Add -lm to your link line. The math library is called libm.a or libm.so. When linking, drop to lib prefix and the .a/.so/.sl suffix and tell the linker that it is a library with a -l. If the linker can't find it, you have to tell it the directory with a -L
 
It can also be -lmath sometimes, I believe.

Header files just contain declarations for the functions you use in your code. The actual code for most of those functions in is a library called libc, which automatically gets linked in with a typical compile & link.

The math library's code isn't with the regular C code, which is why you have to explicitly link it in.

So you're not going crazy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top