Thread: sqrt

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    sqrt

    Code:
    #include <math.h>
    
    float sqr(float a)
    {
          return a*a;
    }
    float length(float x1,float y1,float x2,float y2)
    {
          return sqrt(sqr(x2-x1)+sqr(y2-y1));
    }
    i get the following message from the compiler:
    Code:
    Undefined                       first referenced
     symbol                             in file
    sqrt                                /var/tmp/ccHwDVgc.o
    main                                /usr/local/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    I know that sqrt is defined as double sqrt(double).
    Could someone help me with the casting?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    As I see it you have two choices:

    Code:
    #include <math.h>
    
    float sqr(float a)
    {
      return a*a;
    }
    
    float length(float x1,float y1,float x2,float y2)
    {
      return (float)sqrt((double)sqr(x2-x1)+(double)sqr(y2-y1));
    }
    or

    Code:
    #include <math.h>
    
    double sqr(double a)
    {
      return a*a;
    }
    
    double length(double x1,double y1,double x2,double y2)
    {
      return sqrt(sqr(x2-x1)+sqr(y2-y1));
    }
    The choice is yours.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    dont forget to include the maths library when u compile
    so something like:

    gcc -Wall -ansi filename.c -o filename -lm

    'l' as in 'L'
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    Thanks a lot it works perfectly now. kurz7 do you know why i didn't have to include stdio in the gcc command for other programs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to sqrt
    By shamma in forum C Programming
    Replies: 1
    Last Post: 05-09-2009, 01:42 PM
  2. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  3. sqrt() function help
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 09:07 PM
  4. SQRT Mystery...
    By KneeLess in forum C Programming
    Replies: 7
    Last Post: 03-23-2004, 07:49 AM
  5. how sqrt ?
    By sambs1978 in forum C Programming
    Replies: 3
    Last Post: 09-20-2001, 08:14 AM