对“pow"和“floor"的未定义引用 [英] Undefined reference to `pow' and `floor'

查看:39
本文介绍了对“pow"和“floor"的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 制作一个简单的斐波那契计算器,但是在编译 gcc 时告诉我我缺少 pow 和 floor 函数.怎么了?

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?

代码:

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

输出:

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

推荐答案

你需要用链接标志-lm编译,像这样:

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

gcc fib.c -lm -o fibo

这将告诉 gcc 将您的代码链接到数学库.请务必将标志放在要链接的对象之后.

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.

这篇关于对“pow"和“floor"的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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