drum drum - 1 year ago 55
C Question

Linking with gcc and -lm doesn't define ceil() on Ubuntu

I am currently using gcc to compile and I need to use

Problem is that it won't recognize the library.
I have also tried
and nothing.
The function I tried to use was
and I get the following error:

: undefined reference to `ceil'
collect2: ld returned 1 exit status

I am using the latest Ubuntu and math.h is there.
I tried to use
in a different computer and it work perfectly.

Does anyone know how to solve this problem?

I did include
. Also, the command I used was:

gcc -lm -o fb file.c

Answer Source

Take this code and put it in a file ceil.c:

#include <math.h>
#include <stdio.h>
int main(void)
    printf("%f\n", ceil(1.2));
    return 0;

Compile it with:

$ gcc -o ceil ceil.c
$ gcc -o ceil ceil.c -lm

One of those two should work. If neither works, show the complete error message for each compilation. Note that -lm appears after the name of the source file (or the object file if you compile the source to object before linking).