未定义参考`战俘'和'地板' [英] Undefined reference to `pow' and `floor'

查看:135
本文介绍了未定义参考`战俘'和'地板'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在C简单的计算器斐波纳契但编译时 GCC 告诉我,我错过了战俘和地板的功能。怎么了?

code:

 的#include<&stdio.h中GT;
#包括LT&;&math.h中GT;INT FIBO(INT N);诠释主(){
        输出(纤维蛋白原(4)=%d个,FIBO(4));
        返回0;
}INT FIBO(INT N){
        双披= 1.61803399;        回报(INT)(楼((浮点)(POW(PHI,N)/的sqrt(5))+ .5f));
}

输出:

 瞎扯@ testvm:〜/工作/ C / FIBO $ GCC fib.c -o FIBO
/tmp/ccNSjm4q.o:在功能`FIBO:
fib.c :(文字+ 0x4a):未定义引用'战俘'
。fib.c :(文字+ 0x68)中:未定义引用'地板'
collect2:劳工处返回1退出状态


解决方案

您需要用标志来编译 -lm ,就像这样:

  GCC fib.c -lm -o FIBO

这会告诉GCC来对数学的lib链接您code。只是一定要放标志的您想要的对象链接。

I'm trying to make a simple fibonacci calculator in C but when compiling gcc tells me that I'm missing the pow and floor functions. What's wrong?

Code:

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

int fibo(int n);

int main() {
        printf("Fib(4) = %d", fibo(4));
        return 0;
}

int fibo(int n) {
        double phi = 1.61803399;

        return (int)(floor((float)(pow(phi, n) / sqrt(5)) + .5f));
}

Output:

gab@testvm:~/work/c/fibo$ gcc fib.c -o fibo
/tmp/ccNSjm4q.o: In function `fibo':
fib.c:(.text+0x4a): undefined reference to `pow'
fib.c:(.text+0x68): undefined reference to `floor'
collect2: ld returned 1 exit status

解决方案

You need to compile with the flag -lm, like this:

gcc fib.c -lm -o fibo

This will tell gcc to link your code against the math lib. Just be sure to put the flag after the objects you want to link.

这篇关于未定义参考`战俘'和'地板'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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