-1

我想做一个涉及sqrt(),floor()和的简单函数pow()。所以,我包括<math.h>. 当我尝试使用我的功能时,我的程序会这样说sqrt()并且floor()不存在。我已经三次检查了我的文件并重写了它们,但它仍然给出了同样的错误。只是为了检查目录是否有任何问题<math.h>,我制作了另一个单独的文件来计算相同的东西并且它有效。我现在一头雾水。我究竟做错了什么?

非功能程序的代码:

#include <math.h>
#include "sumofsquares.h"

int sumofsquares(int x){
   int counter = 0;
   int temp = x;

   while(temp != 0){
      temp = temp - (int)pow(floor(sqrt(temp)), 2);
      counter ++;
   }
    return counter;
}

工作测试文件:

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

int main(void){
   printf("%d", (int)pow(floor(sqrt(3)), 2));
}

错误是这样的

/tmp/ccm0CMTL.o:在函数 sumofsquares' 中:/home/cs136/cs136Assignments/a04/sumofsquares.c:9:未定义对 sqrt 的引用'/home/cs136/cs136Assignments/a04/sumofsquares.c:9:未定义引用楼层'collect2:ld返回1退出状态`

我在虚拟 Ubuntu 操作系统上使用 runC 进行编译

4

1 回答 1

8

您可能缺少链接数学库所需的-lm参数。gcc尝试:

gcc ... <stuff> ... -lm

至少有两个与您的问题相关的 C 常见问题解答:

于 2012-02-22T17:14:22.953 回答