math.h problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yuki Tui
    New Member
    • Nov 2009
    • 2

    math.h problems

    newbie warning

    OS Ubuntu, I use a couple of different IDEs and most of them give different compiling warnings for this when compiled as c, as c++ it works

    undefined reference to `trunc'
    warning: implicit declaration of function ‘trunc’
    warning: incompatible implicit declaration of built-in function ‘trunc’

    and trunc is not the only one, I have problems with round(), floor() and exp()
    I searched around, but can't seem to find a solution

    [CODE=c]#include <stdio.h>
    #include <math.h>

    double cio(double a);

    int main(void)
    {
    ///
    return 0;
    }

    double cio(double a)
    {
    double c1=trunc(a);
    while(c1>1)
    c1/=10;
    return c1;
    }[/CODE]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    There is no trunc or round function in the standard library which is why you are getting the errors which are

    undefined reference to `trunc'
    The linker could not find a trunc function when it tried to resolve all references in the program.

    warning: implicit declaration of function ‘trunc’
    trunc has been called without being declared in a header file.

    warning: incompatible implicit declaration of built-in function ‘trunc’
    Seems to be that there is a function trunc but that the implicit declaration supplied above goesn't match the actual declaration of trunc.


    I suspect you are using a third part library or platform specific library but have forgotten to include the required header.

    Why it then works in C++ I could not say given the current information.

    Comment

    • Yuki Tui
      New Member
      • Nov 2009
      • 2

      #3
      warning language (as in having problems expressing/explaining the situation correctly )

      The problem with this is that it is a homework assignment, and that I have to post the answer(code) on the web. Then it gets compiled there and shows the same warnings as the ones listed above. It's not a judge, but I'm afraid they wont look at the code if it has warnings in it.

      I tried it on Win, ide Dev c++ and it works without the warnings

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        Add the -lm option (i.e. -l=link, m=math library) to your compiler command. This will link in the math library during the link stage of compilation.

        Don't feel bad, you are not the first person have this problem. This is one of the oddities of the unix/linux development.

        Comment

        Working...