在C中包含文件
我想创建一个涉及sqrt()
, floor()
和pow()
的简单函数。 所以,我包括 。 当我尝试使用我的函数时,我的程序说
sqrt()
和floor()
不存在。 我已经对我的文件进行了三次检查并重写了它们,但它仍然会出现同样的错误。 只是为了检查目录是否有任何问题,我创建了另一个单独的文件来计算相同的东西并且它有效。 我现在很无能为力。 我究竟做错了什么?
非运作程序的代码:
#include #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 #include int main(void){ printf("%d", (int)pow(floor(sqrt(3)), 2)); }
错误就是这个
/tmp/ccm0CMTL.o:函数sumofsquares’:/ home / cc136 / cs136Assignments / a04 / sumofsquares.c:9:对sqrt’/home/cs136/cs136Assignments/a04/sumofsquares.c:9的未定义引用:undefined reference to楼层’collect2:ld返回1退出状态`
我在虚拟Ubuntu OS上使用runC进行编译
您可能缺少链接数学库所需的gcc
的-lm
参数。 尝试:
gcc ... ... -lm
至少有两个与您的问题相关的C常见问题解答:
- 14.3
- 13.26