私は単純なフィボナッチ計算機をC言語で作成しようとしていますが、コンパイル時にはgcc
はpowとfloor関数がありません。どうしましたか?`pow 'と` floor'への未定義の参照
コード:
#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));
}
出力:これはと照らし合わせてコードをリンクするためにはgccを教えてくれます
gcc fib.c -lm -o fibo
:
[email protected]:~/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
浮動小数点精度は無限ではないので、これは 'n 'の値がそれほど大きくないために間違った答えを与えるでしょう – vonbrand