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 link 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.