You are not logged in.

#1 2013-05-19 10:49:52

mcool4151
Member
Registered: 2013-05-09
Posts: 37

[SOLVED]math.h sqrt error

I wrote a program that uses sqrt function from math.h header file

here's the source code

//sqrtchk.c
#include<stdio.h>
#include<math.h>
int main()
{
int a;
double b;
puts("Enter a number that is greater than 0:");
scanf("%d",&a);
b=sqrt((double)a);
printf("Square Root is %lf",b);
return(0);
}

i compiled it with this command and got this error

[mcool@ArchLinux tests]$ gcc sqrtchk.c -o sqrt
/tmp/ccagMCOp.o: In function `main':
sqrtchk.c:(.text+0x30): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status


what should i do?

Last edited by mcool4151 (2013-05-19 12:50:11)

Offline

#2 2013-05-19 11:17:33

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: [SOLVED]math.h sqrt error

You need to link to libm. Add "-lm" to your options.

Offline

#3 2013-05-19 12:49:38

mcool4151
Member
Registered: 2013-05-09
Posts: 37

Re: [SOLVED]math.h sqrt error

Thanks.......

Offline

#4 2013-05-19 13:05:11

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: [SOLVED]math.h sqrt error

Welcome to the forums mcool4151.

Moving from Applications & Desktop Environments to Programming & Scripting.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB