[lug] OT: cc question

Ed Hill ed at eh3.com
Wed Dec 3 08:07:27 MST 2003


On Wed, 2003-12-03 at 03:58, Stephen Queen wrote:
> If I enter the following simple program
> 
> #include <math.h>
> #include <stdio.h>
> 
> int main()
> {
>         double results;
> 
>         results=sqrt(4);
>         printf ("%f\n", results);
> 
>         return 0;
> }
> 
> and compile it with the following on the command line
> 
> cc simple.c
> 
> I get the following error
> 
> /tmp/cc40XqaS.o(.text+0x1b): In function `main':
> : undefined reference to `sqrt'
> collect2: ld returned 1 exit status
> 
> If I then use the following command to compile it, it works correctly.
> cc simple.c -O2
> 
> This is true on debian and slackware. Does anyone know of an explaination?


Yes, whenever using functions declared in "<math.h>" you should also use
"-lm" at the end to link against the math library such as:

  cc -o simple  simple.c -lm

If you don't, then the linker ("ld") will complain about unresolved
symbols.

Ed

-- 
Edward H. Hill III, PhD
office:  MIT Dept. of EAPS;  Room 54-1424;  77 Massachusetts Ave.
            Cambridge, MA 02139-4307
email:   eh3 at mit.edu,  ed at eh3.com
URL:     http://web.mit.edu/eh3/
phone:   617-253-0098
fax:     617-253-4464
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20031203/6680f436/attachment.pgp>


More information about the LUG mailing list