我想制作一个涉及 sqrt()
、floor()
和 pow()
的简单函数。因此,我包含了
。当我尝试使用我的函数时,我的程序说 sqrt()
和 floor()
不存在。我已经三次检查了我的文件并重写了它们,但它仍然给出相同的错误。为了检查
目录是否有任何问题,我创建了另一个单独的文件来计算相同的内容并且它有效。我现在一无所知。我做错了什么?
非功能程序的代码:
#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 进行编译
I want to make a simple function involving sqrt()
, floor()
and pow()
. So, I included <math.h>
. When I try to use my function, my program says that sqrt()
and floor()
do not exist. I've triple checked my files and rewritten them, but it still gives the same error. Just to check if there was anything wrong with the <math.h>
directory, I made another separate file that calculated the same thing and it worked. I am clueless right now. What am I doing wrong?
The code of the non functioning program:
#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;
}
The working test file:
#include <stdio.h>
#include <math.h>
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
the error is this
/tmp/ccm0CMTL.o: In function sumofsquares':
/home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined reference
to sqrt' /home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined
reference to floor' collect2: ld returned 1 exit status`
I am using runC on a virtual Ubuntu OS to compile
发布评论
评论(1)
您可能缺少链接数学库所需的
gcc
的-lm
参数。尝试:至少有两个与您的问题相关的 C 常见问题解答:
You're probably missing the
-lm
argument togcc
, required to link the math library. Try:There are at least two C FAQs relevant to your problem: