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 asked this code before but then I did not specify how I compiled this program. A person suggested to include -lm while compiling when using gcc. But,

I compiled it using makefile

>>cmake .
>>make
>>./hello.out
Pra
  • 39
  • 8

2 Answers2

1

You need to link your executable with the math library.

For example:

gcc hello.c -o hello -lm
s7amuser
  • 827
  • 6
  • 18
1

Most mathematical functions live in their own library that needs to be linked with the main program. This library is aptly called m.

So you need to add a target_link_libraries to your CMakeLists.txt file to include the m library.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621