[lug] OT: cc question

Scott Herod herod at dimensional.com
Wed Dec 3 09:43:49 MST 2003


On Wed, 3 Dec 2003, 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?

So others have suggested that you should load the math library.  A more
interesting question is why you don't have to do so when you compile with
optimization.  I suspect that if you dig around though the header files in
/usr/include you will find that the sqrt() call is inlined.




More information about the LUG mailing list