0

I did the following code using ceil()

#include<stdio.h>
#include<math.h>    
int main()
{
  float val;
  float cVal;

  val=23.4;

  cVal =ceil(val);
  printf("ceil value:%f\n",cVal);
  return 0;
}

I am getting th following error In function main': test1.c:(.text+0x1b): undefined reference toceil' collect2: error: ld returned 1 exit status

What is wrong in this code??Please help!

I compiled it using makefile

>>cmake .
>>make
>>./hello.out
Pra
  • 39
  • 8
  • What compiler and toolchain are you using? What command did you use to build it? – Dai Feb 18 '20 at 05:19
  • add `-lm` (for `libmath`) to your compile string. Notice the `"Link with -lm."` in [man 3 ceil](http://man7.org/linux/man-pages/man3/ceil.3.html) Nothing wrong with your code, but there is something missing in your compile options. – David C. Rankin Feb 18 '20 at 05:20
  • Including the error message is a good thing, but try to google it yourself. When I searched for "undefined reference to ceil" I found the answer immediately. – klutt Feb 18 '20 at 08:30
  • I complied it using make file..>>cmake . >>make >>/hello.out @Dai – Pra Feb 18 '20 at 08:44

1 Answers1

1

From https://askubuntu.com/a/745199/513302

If you are going to compile a C program with math.h library in Linux using gcc you will have to use –lm option in the compiler command-line

gcc xyz.c -o xyz -lm

The -lm option is actually -l (for "link a module") and m is shorthand for the built-in math library.

Dai
  • 141,631
  • 28
  • 261
  • 374