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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first time using math in C-sqrt problem

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR
Sorry if this is dumm, but I am trying to use sqrtf
from gcc and get:

/tmp/cckMAaOo.o(.text+0x2c): In function `main':
: undefined reference to `sqrtf'
collect2: ld returned 1 exit status

What am I doing wrong? In man sqrt it says you need math.h
and double sqrt, float sqrtf

Here is the code

#include <stdlib.h>
#include <math.h>
main(){
double x=100;
float y=100;
// double sqrt(double x);
// float sqrtf(float x);
printf("sqrt of %f is %f \n",y,sqrtf(y));}
// printf("sqrt of %f is %f and of %f is %f\n",x,sqrt(x),y,sqrt(y));}


 
You invoke the compiler like this
[tt]gcc prog.c -lm[/tt]
That's -LM in lower case.

It tells the linker how to resolve math library functions.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top