Undefined Reference To Ceil

Posted on  by 

  • Because ceil is a static method of Math, you always use it as Math.ceil, rather than as a method of a Math object you created (Math is not a constructor).
  • C library function - ceil - The C library function double ceil(double x) returns the smallest integer value greater than or equal to x.
  • That guide is a copy of the one from here & also doesn't reflect some changes that were recently made. The changes came from discussions we had about installing static libs & includes to /usr/local & the potential issues that doing so may cause.
  1. Perl Ceil
  2. Undefined Reference To Symbol 'ceil@@glibc_2.2.5'
Undefined

The function I tried to use was ceil 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 -lm in a different computer and it work perfectly. Does anyone know how to solve this problem? An alternative situation arises where the source for foo is in a separate source file foo.c (and there's a header foo.h to declare foo that is included in both foo.c and undefinedreference.c). Then the fix is to link both the object file from foo.c and undefinedreference.c, or to compile both the source files.

Example

Undefined Reference To Ceil

One of the most common errors in compilation happens during the linking stage. The error looks similar to this:

So let's look at the code that generated this error:

We see here a declaration of foo (int foo();) but no definition of it (actual function). So we provided the compiler with the function header, but there was no such function defined anywhere, so the compilation stage passes but the linker exits with an Undefined reference error.
To fix this error in our small program we would only have to add a definition for foo:

Undefined reference to mainUndefined reference to ceiling light

Now this code will compile. An alternative situation arises where the source for foo() is in a separate source file foo.c (and there's a header foo.h to declare foo() that is included in both foo.c and undefined_reference.c). Then the fix is to link both the object file from foo.c and undefined_reference.c, or to compile both the source files:

Or:

A more complex case is where libraries are involved, like in the code:

The code is syntactically correct, declaration for pow() exists from #include <math.h>, so we try to compile and link but get an error like this:

This happens because the definition for pow() wasn't found during the linking stage. To fix this we have to specify we want to link against the math library called libm by specifying the -lm flag. (Note that there are platforms such as macOS where -lm is not needed, but when you get the undefined reference, the library is needed.)

Undefined

So we run the compilation stage again, this time specifying the library (after the source or object files):

And it works!

Perl Ceil


Undefined Reference To Symbol 'ceil@@glibc_2.2.5'


Coments are closed