Important instructions for programs using math.h library function

if your program uses math.h header file then sometimes the compilation will lead to errors.In such cases you have to suffix -lm to the compilation file command

Usage

cc filename.c -lm


lm means link mathematical header file.Note that this step is not always required use it only if your program is correct and compilation is leading to errors.
and all other procedures are same.

ex:
consider the following program


#include
#include
main()
{
float num,sq;
printf("Enter the number\n");
scanf("%f",&num);
sq=sqrt(num);
printf("Sqrt=%f",sq);
}


suppose the name of this c file is adi.c
then sometimes compilation of this file will lead to an error like this


bash-2.05b$ cc adi.c
/tmp/cck2Ps76.o(.text+0x42): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status


In such case compile like this


bash-2.05b$ cc adi.c -lm

About This Blog

There is something for everyone. !! A tech blog on various topics. Let it be devops, programming etc; we have it all covered here in layman terms :)

Total Pageviews

Back to TOP