Thread: Problem with sqrt function

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    Problem with sqrt function

    I have 2 integer variables which I want to multiply then find the square root (the geometric mean). This result needs to be saved in a double variable.

    I have tried typecasting them, but something about my function isn't working right. I always get this error

    Code:
    Undefined                       first referenced
     symbol                             in file
    sqrt                                /tmpsort/ccAKaa8v.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    At first I thought I was typecasting wrong, but when I removed all of that and simply tried to do sqrt on a single double variable, I still hit the error.

    Code:
    double d, e;
    
    d = 3.2;
    e = sqrt(d);
    I don't get the error if I do sqrt on a number.

    Code:
    double e;
    e = sqrt(3.2);
    I have included math.h.

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Phoenix, AZ
    Posts
    2
    What compiler are you using? I know that when I compile with GCC, I have to include the -lm flag to include the math library, like so:

    Code:
    gcc myprog.c -o myprog -lm
    -Keith

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    Quote Originally Posted by kjerosky View Post
    What compiler are you using? I know that when I compile with GCC, I have to include the -lm flag to include the math library, like so:

    Code:
    gcc myprog.c -o myprog -lm
    -Keith
    Thank you, this worked..

    That's pretty annoying that I'll have to include that tag everytime I compile something that uses math.h though. I might just write my own sqrt function

    Do you know if this is the case for any other library? And why is math.h like this?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Phoenix, AZ
    Posts
    2
    No problem - I do this to myself every so often, and I've been programming for a long time. :P I can't think of many other times where I've had to use the -l flag to include standard C libraries besides the math one, though when I create my own libraries I definitely have to use it. Here's a nice link to why math.h requires the -lm flag: compile - Why do you have to link the math library in C? - Stack Overflow

    Good luck!

    -Keith

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only library that is automatically linked by default is the C runtime (which contains everything in the standard C libraries and then some); all the rest require -l.

    Historically, the math library was kept separate so that people could choose between "fast" and "accurate" back when it wasn't possible to do both at the same time. However, I haven't been on a system where I've needed to use -lm for a while, and I don't know that I'm doing anything special. Presumably that means that whoever installed/set up your system doesn't like you. (Okay, maybe that's not the reason.)

    (EDIT: No wait, I do for things like sqrt. I had forgotten which things, and looked at the wrong source. Sorry.)
    Last edited by tabstop; 07-29-2011 at 12:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ld problem in sqrt function
    By brack in forum C Programming
    Replies: 2
    Last Post: 10-26-2010, 06:52 AM
  2. sqrt function
    By kbpsu in forum C++ Programming
    Replies: 16
    Last Post: 04-02-2009, 11:32 PM
  3. sqrt() function help
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 09:07 PM
  4. Sqrt() Function
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 04-22-2003, 04:31 PM
  5. sqrt function
    By Extol in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2003, 06:04 AM