# Thread: Compiling some maths examples

1. ## Compiling some maths examples

Hi all,

I have been running through the basics math.h examples in c.

Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
double x = 2.25;
double less = 0.0;
double more = 0.0;
double root = 0.0;

double angle = 45.0;
double pi = 3.1419265;
double sine = 0.0;
double cosine = 0.0;

printf("\nExamples in maths.h - x = 2.25\n");

less = floor(2.25);
printf("\nless = floor(x) is %.2f",less);

more = ceil(2.25);
printf("\nmore = ceil(x) is %.2f",more);

root = sqrt(2.25);
printf("\nroot = sqrt(x) is %.2f",root);

// sine = sin(pi*angle/180.0);
printf("\nsin(%.6f*%.2f/180.0) is sin(%.3f) = %.3f\n",pi, angle, sine);

return 0;
}```
All makes perfect sense. When compiling though... it complains if put x instead of 2.25 in each of the functions. - while in the generated object file.

[jryan@freedom lessons]\$ gcc 2.20_mathExamples.c -o 2.20_mathExamples
/home/jryan/tmp/ccvACeHe.o: In function `main':
2.20_mathExamples.c.text+0xb2): undefined reference to `floor'
collect2: ld returned 1 exit status

So take the error about floor for example... I had the other functions remmed out at the time of the above error - they all error with the same message. So, if I hardcode 2.25 instead of x in floor like in the code above. eg floor(2.25) instead of floor(x), it works! Hmm, what I am I doing wrong here? Similarly it does not recognise sign even though I did include math.h.

2. You need to link in the math library. Add -lm to the command line to compile/link.

3. Originally Posted by grumpy