在c包括文件 [英] Including files in C

查看:90
本文介绍了在c包括文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要作出有关的sqrt()地板() POW()。所以,我包括<&math.h中GT; 。当我尝试使用我的功能,我的计划说,的sqrt()地板()不存在。我三重检查我的文件,并改写它们,但它仍然给出了同样的错误。只是为了检查,如果有什么不对的<&MATH.H GT; 目录中,我做了计算的同样的事情的另一个单独的文件和它的工作。我现在很无能。我在做什么错了?

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?

非运作项目的code:

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;
}

工作测试文件:

#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:未定义的引用
  到开方'/home/cs136/cs136Assignments/a04/sumofsquares.c:9:未定义
  参照地板'collect2:LD返回1退出status`

/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`

我使用一个虚拟的Ubuntu操作系统runC编译

I am using runC on a virtual Ubuntu OS to compile

推荐答案

您很可能缺少 -lm 参数 GCC ,以数学库链接要求。尝试:

You're probably missing the -lm argument to gcc, required to link the math library. Try:

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

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

There are at least two C FAQs relevant to your problem:

  • 14.3
  • 13.26

这篇关于在c包括文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆