conline-compilationgcc7

Is this a glitch with online C compiler, gcc-7.2.0?


On an online C compiler called jdoodle, I tried this simple snippet below:

#include<math.h>
#include<stdio.h>

int main(void)
{
    double f = 1.2;
    //printf("%f\n", ceil(f));
    printf("%f\n", ceil(1.2));
    return 0;
}

It prints:

2.000000

Which is what I expected.

But when I change code to:

printf("%f\n", ceil(f));  
//printf("%f\n", ceil(1.2));

The compiler complains:

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

Its fairly simple and clear from the man page for ceil() that it takes a double variable as the only argument.

When I changed the compiler version to 5.3.0 from 7.2.0, both codes were compiled successfully and generated the expected output.

Why is the updated version of compiler complaining about it then?

If compiler is right about complaining it, can anyone tell me why ceil(f); would be a problematic piece of code so that gcc-7.2.0 is not considering it valid, surprisingly assigning 'an undefined reference error' to a valid library function?

Update: I tried the same snippet with codechef online compiler with C-GCC6.3, it compiles fine and generates the expected output.


Solution

  • The man page for ceil(3) document that:

    you need to #include <math.h>

    and that you should

    Link with -lm.

    You forgot to configure your online compiler to link with -lm; perhaps the one you are using don't offer such an option.

    I recommend compiling on your own computer.