Here's the thing, 99.99999% of the time, when someone says "this basic function that's available to the world doesn't work" they're wrong. The fraction of the time when something this basic breaks, there's already an army with pitchforks somewhere.
#include <math.h>
#include <stdio.h>
int partSize(int n){
return log2(n);
}
int main(int argc, char *argv[]) {
int ret = -1;
ret = partSize(16);
printf("%d\n", ret);
return 0;
}
Compile with:
> gcc -std=c99 a.c -o log2.out -lm
> ./log2.out
> 4
Yup, it's working.
In C, using a previously undeclared function constitutes an implicit declaration of the function. In an implicit declaration, the return type is int
. So the error tells you that log2()
was not defined in your code that leads to some issue in the code you didn't post.
When I skip the -lm
I get:
a.c:(.text+0x11): undefined reference to `log2'
collect2: ld returned 1 exit status
..that doesn't look right. OK, when I add the -lm
but remove the #include <math.h>
I get:
a.c: In function ‘partSize’:
a.c:5:5: warning: implicit declaration of function ‘log2’ [-Wimplicit-function-declaration]
Hey, there's your warning! So you're probably correct that you're including the -lm
but for some reason the #include math.h
has a problem. Could be that:
- math.h is missing
- you didn't really include it in the file, is it in a #def and being compiled out for example?
- Your version of math.h doesn't define log2