[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: c, gcc, ld?



mulix <mulix@ibm.net> writes:

> I have the following code snippet:
> 
> #include <stdio.h>
> #include <math.h>
> 
> int main(void)
> {
>      double i;
>      i = sqrt(9);
>      printf("%e\n",i);
>      return 0;
> }
> 
> With g++, it compiles fine.
> With gcc, I get:
> "... undefined reference to 'sqrt'"
> "collect2: ld returned 1 exit status code"

> So... what am I missing? Is it gcc, ld, or the code is simply wrong?

The diagnostics actually tell you it's ld. Add -lm to your linking
rule, that is where sqrt lives.

-- 
Oleg Goldshmidt | BLOOMBERG L.P. (BFM) | oleg@bfr.co.il
"... We work by wit, and not by witchcraft;
 And wit depends on dilatory time." - W. Shakespeare.

=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il