How to use sqrt() function with gcc?



I'm trying to use the sqrt() function in Linux with GCC v3.3.6, compiling with this command:
$ gcc sourcefile.c

I get this error:
In function 'main': undefined reference to 'sqrt'
collect2: ld returned 1 exit status

Here is my source code:
#include <stdio.h>
#include <math.h>
main()
{
double num, sq_double;
scanf("%d", &num);
sq_double = sqrt(num);
}

What am I doing wrong? Is there something special I need to do to use the math.h functions?
.



Relevant Pages

  • Re: linking issue on Redhat 9 with cryptlib 3.1
    ... Bert Szoghy wrote: ... >: undefined reference to `cryptInit' ... compile command. ... gcc -o bert bert.c cryptlib.a ...
    (sci.crypt)
  • Re: can not figure out the problem
    ... /tmp/ccAxyQSj.o: In function `main':: undefined reference to `sqrt' collect2: ld returned 1 exit status ... At first I thought is because the gcc "math.h" do not have the function ...
    (comp.lang.c)
  • Undefined reference error
    ... Trying to compile a program using GCC and written in C. ... "sqrt" function and I keep getting an "undefined reference" error even ...
    (alt.os.linux.suse)
  • Re: Where are the math functions in FC4?
    ... When I compile using gcc I get an "undefined reference ... to sqrt" message. ... Add "-lm" on the GCC ...
    (linux.redhat)
  • Where are the math functions in FC4?
    ... I created a very simple c program using sqrt(), ... When I compile using gcc I get an "undefined reference to sqrt" ... Other math functions such as sin or cos also do not work. ...
    (linux.redhat)