Search code examples
cmathtruncation

Why the trunc function of C shows me an error?


When i tried to run the trunc function in C it gives me an error that says undefined reference to `trunc' how to resolve this error?

the error code is here:

/usr/bin/ld: /tmp/ccTANPox.o: in function `floatmod': temp1.c:(.text+0x3a6): undefined reference to `trunc' collect2: error: ld returned 1 exit status


Solution

  • if I compile code below with gcc main.c, then I see this error (which is similar to yours)

    /tmp/ccbouRK0.o: In function `main':
    main.c:(.text+0x23): undefined reference to `trunc'
    collect2: error: ld returned 1 exit status
    

    and compile works with gcc -lm main.c (where you see how to use -lm).

    #include <math.h>
    #include <stdio.h>
    int main()
    {
       double f=9.8;
       int i;
       i=trunc(f);
       printf("%i",i);
       return 0;    
    }