3

I have a function with the following code:

static void
calculate_lifetime(uint16_t p_weight){
  double param, result;
  param = (double) p_weight;
  result = log (param);
}

I include the math library, but when I compile I get the error:

undefined reference to `log'

Am I doing something wrong when casting? When I remove param and put a number like 8 it compiles correctly.I'm compiling for Contiki.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
CodePorter
  • 230
  • 1
  • 3
  • 13
  • 3
    When you compile, do you link to the math library? Is there a `-lm` option? – Eric Jablow Feb 22 '14 at 20:03
  • 5
    Just including `` is not enough, you also have to link the math library (with `-lm` for gcc). – M Oehm Feb 22 '14 at 20:03
  • You're all right :). I'll post an answer for anyone using Contiki. I had to update the Contiki Make file and the corresponding cpu Make file. – CodePorter Feb 22 '14 at 20:57
  • @CodePorter if you use a constant many compilers can do the [calculation at compile time](http://stackoverflow.com/questions/19230849/is-maths-library-included-in-the-glibc-now/19230882#19230882) and thus avoid linking with `math.h`. – Shafik Yaghmour Feb 22 '14 at 21:18
  • 3
    Remember that the `-lm` has to *follow* the source file name on the compiler command line. – Keith Thompson Feb 22 '14 at 22:08
  • With the code shown, the variable `result` is never used, so the compiler might end up optimizing the whole function body away -- and since it is static, it could simply not call the function and not generate any code for it. If the function returned the result, all would be well. – Jonathan Leffler Jan 23 '15 at 18:16

0 Answers0