Thread: gcc saying --------->undefined reference to sqrt

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Lightbulb gcc saying --------->undefined reference to sqrt

    Code:
    #include<stdio.h>
    #include<math.h>
    void algo(int);
    main()
    {
            algo(5);
    }
    void algo(int k)
    {
            printf("%f",sqrt(k));
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You need to link to the math library explicitly...
    by the
    -lm
    flag for ld.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If only there was a handy readily available reference for this..........
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by AndrewHunter View Post
    If only there was a handy readily available reference for this..........
    I think you just volunteered to write said FAQ article. I can think of one or two others that I might start writing.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anduril462 View Post
    I think you just volunteered to write said FAQ article. I can think of one or two others that I might start writing.
    Any C++ related topic that I could volunteer to help write one ?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's the To Do list from the FAQ page. The one(s) that I'm considering right now are the "using multiple files in C" and the related, possibly in the same article, explanation/use of the extern keyword. Take anything else on the list that needs doing, or just write one that you feel is needed based on your experience here.

    EDIT: It looks like the FAQs haven't been touched in a while, but I don't see that as a reason to not work on them.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anduril462 View Post
    Here's the To Do list from the FAQ page. The one(s) that I'm considering right now are the "using multiple files in C" and the related, possibly in the same article, explanation/use of the extern keyword. Take anything else on the list that needs doing, or just write one that you feel is needed based on your experience here.

    EDIT: It looks like the FAQs haven't been touched in a while, but I don't see that as a reason to not work on them.
    The sections of that list which could've been interesting for me, are unfortunately empty or already have very good "standard" links to point in answers.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is a new function in C99 that finds the square root of a long.
    Code:
    #define I int
    I chooseYou = sqrtl( 0xDL );
    I might be ashamed of myself if I wasn't chuckling so much.


    Quzah.
    Last edited by quzah; 08-26-2011 at 04:08 PM. Reason: #define fail, too tired
    Hope is the first step on the road to disappointment.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quzah View Post
    There is a new function in C99 that finds the square root of a long.
    Code:
    #define int I
    I chooseYou = sqrtl( 0xDL );
    I might be ashamed of myself if I wasn't chuckling so much.

    Quzah.
    ...SO sqrtl evolved *from* something !

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by quzah View Post
    There is a new function in C99 that finds the square root of a long.
    Code:
    #define int I
    I chooseYou = sqrtl( 0xDL );
    I might be ashamed of myself if I wasn't chuckling so much.


    Quzah.
    Stop chuckling and be ashamed! That code is all kinds of broken. Your define is backwards and sqrtl takes and returns a long double, not an int. That being said, I feel like there is some sort of joke I totally didn't get...

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anduril462 View Post
    ... I feel like there is some sort of joke I totally didn't get...
    Yes..there is..
    Watch some cartoon to find out !

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Ahh, Pokemon. It was after my time, and I don't have kids yet.

    In that case, very well played Quzah. Went right over my head, but I'm short, so it happens a lot.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    Stop chuckling and be ashamed! That code is all kinds of broken. Your define is backwards
    Must have been not fully awake on that one.
    Quote Originally Posted by anduril462 View Post
    and sqrtl takes and returns a long double, not an int.
    I didn't even know there really was a sqrtl (it's nonstandard). I was just making a joke.
    Quote Originally Posted by anduril462 View Post
    That being said, I feel like there is some sort of joke I totally didn't get...
    That's alright, I got the joke.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to 'sqrt'
    By VascoP in forum C Programming
    Replies: 8
    Last Post: 10-07-2009, 10:33 AM
  2. undefined reference to sqrt
    By shamma in forum C Programming
    Replies: 1
    Last Post: 05-09-2009, 01:42 PM
  3. undefined reference to `sqrt' when using math.h
    By Milhas in forum C Programming
    Replies: 4
    Last Post: 03-31-2008, 06:11 PM
  4. undefined reference to 'sqrt'
    By joerg in forum C Programming
    Replies: 9
    Last Post: 04-21-2007, 11:33 AM
  5. Replies: 5
    Last Post: 06-01-2006, 04:37 PM