Undefined Reference To Ceil

Example

: undefined reference to `ceil' collect2: ld returned 1 exit status I've also tried #cc -lm myprogram.c and then tried compiling it, but this didn't help and displayed the same message.

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

  1. Undefined references can have different reasons. Here are some: 1) The most common issue is about the the inline behavior. Clang is following by default the C99 standard while gcc promote GNU89. The following code will build with gcc but fails with.
  2. 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.

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:

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.)

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

And it works!


Undefined Reference To Ceil

The Math.ceil() function always rounds a number up to the next largest integer.

Note: Math.ceil(null) returns integer 0 and does not give a NaN error.

Syntax

Parameters

x
A number.

Return value

The smallest integer greater than or equal to the given number.

Description

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).

Examples

Using Math.ceil()

The following example shows example usage of Math.ceil().

Decimal adjustment

Specifications

Specification
Unknown specification
# sec-math.ceil

Browser compatibility

Undefined Reference To Ceil'

BCD tables only load in the browser

Undefined Reference To Error

See also

Comments are closed.