这个错误是什么意思?

时间:2011-08-12 17:12:56

标签: c gcc compiler-errors

  

/tmp/ccjiJKv2.o:在函数func':
b.c:(.text+0x16b): undefined reference to
sqrt'中   collect2:ld返回1退出状态

这是代码,我正在使用gcc编译器..

/*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) ;

}

3 个答案:

答案 0 :(得分:5)

您没有链接包含sqrt 实现的库。

该库被称为“libm”(数学库),可以链接到如下:

gcc -o myprog infile.c -lm

答案 1 :(得分:3)

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

此外,Link with -lm

答案 2 :(得分:2)

你必须链接数学库。 -lm