相关疑难解决方法(0)

为什么必须在C中链接数学库?

如果我包含<stdlib.h><stdio.h>在C程序中,我不必在编译时链接这些,但我必须链接到<math.h>,使用-lmgcc,例如:

gcc test.c -o test -lm
Run Code Online (Sandbox Code Playgroud)

这是什么原因?为什么我必须显式链接数学库而不是其他库?

c compilation math.h

241
推荐指数
7
解决办法
11万
查看次数

未定义的引用`pow'和`floor'

我正在尝试在C中制作一个简单的斐波那契计算器,但在编译时gcc告诉我,我错过了战俘和地板功能.怎么了?

码:

#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));
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

c gcc undefined-reference

117
推荐指数
5
解决办法
18万
查看次数

标签 统计

c ×2

compilation ×1

gcc ×1

math.h ×1

undefined-reference ×1