我想做一个简单的函数涉及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;
}
Run Code Online (Sandbox Code Playgroud)
工作测试文件:
#include <stdio.h>
#include <math.h>
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
Run Code Online (Sandbox Code Playgroud)
错误就是这个
/tmp/ccm0CMTL.o:在函数sumofsquares'中:/home/cs136/cs136Assignments/a04/sumofsquares.c:9:对sqrt'/home/cs136/cs136Assignments/a04/sumofsquares.c:9的未定义引用:undefined reference to floor'collect2:ld返回1退出状态`
我在虚拟Ubuntu OS上使用runC进行编译
您可能缺少链接数学库所需的-lm参数gcc.尝试:
gcc ... <stuff> ... -lm
Run Code Online (Sandbox Code Playgroud)
至少有两个与您的问题相关的C常见问题解答: