这个错误是什么意思?

3

/tmp/ccjiJKv2.o: 在函数func'中:
b.c:(.text+0x16b): 对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个回答

5

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

该库被称为“libm”(数学库),可以按照以下方式进行链接:

gcc -o myprog infile.c -lm

gcc: 未识别选项 '-1m' /tmp/ccpWiDRt.o: 在函数 func' 中: b.c:(.text+0x16b): 对 sqrt' 未定义的引用 collect2: ld 返回了状态值 1 - Udit Gupta
请将字体调整为小写的"L",而不是数字1。 - Yann Ramin

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

此外,需要使用 -lm 进行链接。

你不需要“卸载”gcc。只需使用正确的选项,它就可以正常工作。 - hari
@hari 谢谢伙计……它起作用了,但我有一个问题:每当使用新库时,我需要以这种方式链接每个新函数吗? - Udit Gupta
@Udit Gupta:所以,每个库都有它们自己定义的函数,在这个例子中,sqrt 函数在 math 库中。同时,math 库还有很多其他函数。简而言之,是的,你需要链接到你想要使用的函数库。 - hari
我多次遇到了相同的错误...但是我们需要对printf做同样的事情吗?我在另一个程序中遇到了一个错误,即“未定义对'printf'的引用”。 - Udit Gupta
据我所知,printflibc的一部分,它是一个基本/常用库,因此您不需要显式地链接它。请将代码粘贴到另一个问题中以解决该问题。 - hari
显示剩余7条评论

2

您需要链接数学库。 -lm


这是正确的代码,我之前添加了jst库但忘记在此处粘贴...现在这是实际出错的代码。 - Udit Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接