tags:

views:

390

answers:

2

Hi,

I have two questions regarding ceil function..

1. The ceil() function is implemented in C. if i use ceil(3/2), it works fine.. But when i use ceil(count/2)-if value of count is 3, then it gives compile time error..


/tmp/ccA4Yj7p.o(.text+0x364): In function FrontBackSplit': : undefined reference toceil' collect2: ld returned 1 exit status


How to use the ceil function in second case?? Please suggest..

  2. How can i implement my own ceil function in C. 

Please give some basic guidelines.

Thanks.

+2  A: 

The ceil() function is implemented in the math library, libm.so. By default, the linker does not link against this library when invoked via the gcc frontend. To link against that library, pass -lm on the command line to gcc:

gcc main.c -lm
Adam Rosenfield
Note that this is not true on all platforms. Some platforms do not require linking against libm to get math functions.
Dietrich Epp
Hi this works fine for me.. Thanks.. What is this parameter -lm?
AGeek
@RBA, "-lm" means to link against the library "libm.so" or "libm.dylib" or "m.dll", depending on your platform.
Michael Aaron Safyan
+2  A: 

The prototype of the ceil function is:

double ceil(double)

My guess is that the type of your variable count is not of type double. To use ceil in C, you would write:

#include <math.h>
// ...
double count = 3.0;
double result = ceil(count/2.0);

In C++, you can use std::ceil from <cmath>; std::ceil is overloaded to support multiple types:

#include <cmath>
// ...
double count = 3.0;
double result = std::ceil(count/2.0);
Michael Aaron Safyan
No, Its still giving the same error...
AGeek