0

So we have this program in C, we need to use the base-2 logarithmic function, to get the base 2 logarithm of n. Here's the code:

#include <math.h>

int partSize(int n) {
    return log2(n);
}

But when compiling, it gives us the following warning.

sim.c: In function partSize : sim.c:114: warning: incompatible implicit declaration of built-in function log2

This is the command we use

 gcc $file -o $name.out -lm 
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
JoMP
  • 49
  • 1
  • 5
  • You haven't read the warning, nor did you try to understand or to google it, which is bad. Furthermore, this is a duplicate (hundreds of times over). –  May 07 '13 at 17:43
  • 1
    possible duplicate of [How to write log base(2) in c/c++](http://stackoverflow.com/questions/3064926/how-to-write-log-base2-in-c-c) –  May 07 '13 at 17:43
  • yes i did.I googled it and saw lots of them, but in the majority of the cases, the users didn't add the -lm.I have read the other one (How to write log base(2)) but it didn't help – JoMP May 07 '13 at 17:46
  • `double log2(double)`, gcc option `std=c99` use – BLUEPIXY May 07 '13 at 17:49
  • Are you **really** sure you have `math.h` included in your **real** sources? – alk May 07 '13 at 17:55
  • Looks good here http://ideone.com/bV4THd , Can you post the whole program that fails. – Shafik Yaghmour May 07 '13 at 17:57
  • `log2` was added to C by the 1999 ISO standard. The error message implies that your compiler recognizes it, but perhaps you `` is an older version that doesn't declare `log2`. What happens if you call `log` rather than `log2`? (`log` was standard before `log2` was.) And what happens if you compile with `-std=c99`? What system are you on? – Keith Thompson May 07 '13 at 18:41
  • Furthermore, your source file has no `main` function. If you compile it without `-c`, you'll get a linker error. Either that's not your complete source file, or that's not your actual `gcc` command line. And is there anything funny about the values of `$file` and `$name`? Why would you give the output file a `.out` suffix? – Keith Thompson May 07 '13 at 18:44
  • Looks like you need some more analysis. You didn't give a workable example, so the best we can do is reproduce the failure and give suggestions... If you can find a way to make it reproducible then update the question with that... until that time this can't be fully answered. – Mike May 07 '13 at 19:05

1 Answers1

7

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:

  1. math.h is missing
  2. you didn't really include it in the file, is it in a #def and being compiled out for example?
  3. Your version of math.h doesn't define log2
Mike
  • 47,263
  • 29
  • 113
  • 177