Find the answer to your Linux question:
Results 1 to 9 of 9
Hi guys, I'm getting a compiler error "undefined reference to atan" and the same for sqrt when I pass in variables (doubles) . If I simply use a number e.g. ...
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    6

    Smile atan and sqrt

    Hi guys, I'm getting a compiler error "undefined reference to atan" and the same for sqrt when I pass in variables (doubles) . If I simply use a number e.g. atan(2) and sqrt(2) everything compiles fine.
    I would really appreciate some help on this. I'm sure I'm doing something stupid, I just don't know what it is!
    Thanks

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Post your code here, please. And do use code blocks to enclose it so that indentations, and parens are not removed or mis-interpreted ( parens often end up as smileys ). It makes it a lot easier to read. Example:
    Code:
    // This is some C++ code
    #include <iostream>
    
    int main (void)
    {
        // do something
        return 0;
    }
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2011
    Posts
    6

    atan and sqrt

    Here is the code:

    Code:
    void calcVelAndHdg(double northV, double eastV, double upV){
    
     double vel2D, vel3D, hdg2D, tmp2D, tmp3D;
    
     tmp2D = pow(northV,2) + pow(eastV,2);
     tmp3D = pow(upV,2) + tmp2D;
     vel3D  = sqrt(tmp3D)
     vel2D  = sqrt(tmp2D);
     tmp2D = northV/eastV;
     hdg2D = atan(tmp2D);
    
     printf("2D velocity=%f  3D veloocity=%f 2D heading=%f \n\r",
               vel2D, vel3D, hdg2D);
    }
    If i replace tmp3D and tmp2D with, say 2, in the sqrt and atan terms the program compiles fine otherwise I get the "undefined reference to sqrt" and "undefined reference to atan" messages. I also tried using atan2(northV, eastV) but got the same error message and could not get rid of it even substituting numbers.
    Thanks.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Did you #include <math.h>?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    May 2011
    Posts
    6
    Yes i did. Also the text for the functions pow, atan and sqrt (and atan2 when I tried it) was highlighted in red on my dev window indicating they were recognized. Interestingly I had the same problem earlier problem with pow when I mistakenly used tmp2D=pow(northV,northV)+pow(eastV,eastV); but when I replaced with tmp2D=pow(northV,2)+pow(eastV,2); it compiled fine. FYI I am using DigiESP to develop an embedded application on a digi cc9p9215 (arm 9) module, with Ubuntu Linux on my dev computer.
    Thanks.

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    48
    Did you use -lm when you compiled?

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Ok. Are you using the gcc compiler suite, or something else? Some embedded operating systems do not support floating point, so use integer versions of these functions, which only allows approximate solutions. Something to consider. The signature of atan() and pow() for Linux systems are:
    Code:
    double atan(double x);
    double pow(double x, double y);
    You need to see what is in your math.h header.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    May 2011
    Posts
    6

    atan and sqrt

    Sorry, was tied up on something else. No I did not use -lm when I compiled, I tried finding where to put it but couldn't. I'm using the digiESP development interface which is somewhat automated, so I just click on clean and build and the system does the rest. Also haven't yet been able to find the math.h library (obviously it's somewhere on the system but when I do an include the system just automatically finds it!)
    As you can tell, I'm a bit of a novice at this!
    I do know my system supports doubles and floats as I am already successfully using these elsewhere in the system.
    Thanks

  9. #9
    Just Joined!
    Join Date
    May 2011
    Posts
    6
    Hi Rubberman, Just to let you know I finally managed to fix the problem. It was the -lm option in the compilation and after (a lot of) digging around I found where the command line was that I need to include the -lm in.
    Thanks for all your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...