[RndTbl] Using math routines (math.h, header files) in C programs.

Gilbert E. Detillieux gedetil at cs.umanitoba.ca
Tue Feb 4 17:56:58 CST 2003

According to Ihor Jakowec:
>    I was attempting to use the math routines of the libc library.
> The following  program (listed below), would not compile.
> A shell session, showing error messages, follows the source
> code.
> Source code:
> #include <stdio.h>
> #include <math.h>
> int main (void)
> {
>   printf("The square root of 2.0  is:  % .6f\n", sqrtf(2.0));
> }

OK, first problem I can see is the function should be called "sqrt", and not
"sqrtf".  That's the way it's defined on my system anyway.  It's best to
always check the man pages for the functions first, to be sure about their
names and expected argument types.

> Shell session showing error message:
> $ gcc -o example example.c
> /tmp/cc4fVMFv.o: In function `main':
> /tmp/cc4fVMFv.o(.text+0x12): undefined reference to `sqrtf'
> collect2: ld returned 1 exit status
> $

It may also be necessary to include the "-lm" option explicitly, at the end
of the gcc command line, to have it link against the math library.  I don't
think gcc will link against that library by default.

> Setting path and and gcc environment variables did not
> make the program compile.  Is this feature (math.h), fully
> implemented or, unsupported or, depreciated?   Please, tell
> me what to do to get this program to work.

No, setting path and environment variables won't help here.  You'd only have
to do that if the libraries you wanted to use weren't in the usual library
search path.

Hope the above helps.

Gilbert E. Detillieux		E-mail:	<gedetil at cs.umanitoba.ca>
Dept. of Computer Science	Web:	http://www.cs.umanitoba.ca/~gedetil/
University of Manitoba		Phone:	(204)474-8161
Winnipeg, MB, CANADA  R3T 2N2	Fax:	(204)474-7609

More information about the Roundtable mailing list