Udit Gupta
Udit Gupta

Reputation: 3272

What does this error mean?

/tmp/ccjiJKv2.o: In function func':
b.c:(.text+0x16b): undefined reference to
sqrt'
collect2: ld returned 1 exit status

Here is the code and I am using gcc compiler..

/*Write a function that receive 5 integers and returns the sum,average and standard deviation of these numbers*/

/*Author:Udit Gupta     Date:10/08/2011*/

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

void func (int *,float *,float *);

int main () {

        float avg,std_dev;

        int sum;

        func (&sum,&avg,&std_dev);      /*Passing address of variables where output will be stored.....*/

        printf ("The sum of numbers is %d\n",sum);
        printf ("The average of numbers is %f\n",avg);
        printf ("The standard deviation of numbers is %f\n",std_dev);

}


void func (int *sum_, float *avg_ , float * std_dev_) {

        int n1,n2,n3,n4,n5;

        printf("Please enter the number:");
                scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);


        /*Formula for sum,average and standard deviation*/

        *sum_ = n1+n2+n3+n4+n5;                 /*Writing output at the address specified by arguments of function*/
        *avg_ = *sum_ / 5 ;
        *std_dev_ = sqrt ( pow((n1-*avg_),2)+pow((n2-*avg_),2)+pow ((n3-*avg_),2)+pow ((n4-*avg_),2)+pow ((n5-*avg_),2)/4) ;

}

Upvotes: 3

Views: 412

Answers (3)

Yann Ramin
Yann Ramin

Reputation: 33197

You are not linking the library which contains the implementation of sqrt.

That library is known as "libm" (the math library), and can be linked to as follows:

gcc -o myprog infile.c -lm

Upvotes: 5

Karoly Horvath
Karoly Horvath

Reputation: 96306

you have to link the math library. -lm

Upvotes: 2

hari
hari

Reputation: 9743

#include <math.h> - is this what you need?

Also, Link with -lm

Upvotes: 3

Related Questions