Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: Very simple C question (maybe!)

  1. #11
    Join Date
    Oct 2005
    Location
    Queensland, Australia
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Very simple C question (maybe!)

    I've just tried it on Intrepid with Anjuta 2.4.2, whereas before I used Hardy with Anjuta 2.4.1.
    On Interpid it compiles both ways, no problems!

    A note to stroyan.
    The original problem was not to do with sqrt(16.0) or dosomething(), but with line 18:
    d = sqrt(d); // when ERROR: "main.c:18: undefined reference to `sqrt'"
    Thanks guys, Mike

  2. #12
    Join Date
    Oct 2007
    Location
    Fort Collins, CO, USA
    Beans
    481
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Very simple C question (maybe!)

    Quote Originally Posted by mdurham View Post
    I've just tried it on Intrepid with Anjuta 2.4.2, whereas before I used Hardy with Anjuta 2.4.1.
    On Interpid it compiles both ways, no problems!

    A note to stroyan.
    The original problem was not to do with sqrt(16.0) or dosomething(), but with line 18:
    The "d = sqrt(d)" in line 18 is just one more iteration of constant evaluation.
    If the compiler can reduce the previous assignments to d into a constant,
    then it can replace the sqrt and assignment in line 18 with a constant.
    If the value of d before line 18 is potentially variable then the compiler
    needs to really use a function call to sqrt.

  3. #13
    Join Date
    Oct 2005
    Location
    Queensland, Australia
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Very simple C question (maybe!)

    Quote Originally Posted by stroyan View Post
    The "d = sqrt(d)" in line 18 is just one more iteration of constant evaluation.
    If the compiler can reduce the previous assignments to d into a constant,
    then it can replace the sqrt and assignment in line 18 with a constant.
    If the value of d before line 18 is potentially variable then the compiler
    needs to really use a function call to sqrt.
    I think that explains it perfectly. But it's interesting that it works without additional instructions (ie including the maths lib) on Intrepid.
    Cheers, Mike

  4. #14
    Join Date
    Dec 2007
    Beans
    1

    Wink Re: Very simple C question (maybe!)

    When you just use sqrt(16.0) you might not need to link with libm.so (-lm option). This is because sqrt() is a built-in function of gcc (on my machine gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3) and when you call sqrt with a constant, it simply uses the built-in function sqrt.

    You can disable all built-in functions with -fno-builtin option of gcc, or specifically in the case of sqrt with the option -fno-builtin-sqrt.
    Then it does complain about "undefined reference to `sqrt'". If we use either of these options, we need to link with -lm to resolve the symbol sqrt.

    But when you use a variable for invoking sqrt, gcc does not use any built-in function. It tries to call the function sqrt, which is typically defined in libm.so forcing us to use -lm always.

    Hope this clarifies the issue.

  5. #15
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Very simple C question (maybe!)

    Quote Originally Posted by sajithn View Post
    When you just use sqrt(16.0) you might not need to link with libm.so (-lm option). This is because sqrt() is a built-in function of gcc (on my machine gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3) and when you call sqrt with a constant, it simply uses the built-in function sqrt.

    You can disable all built-in functions with -fno-builtin option of gcc, or specifically in the case of sqrt with the option -fno-builtin-sqrt.
    Then it does complain about "undefined reference to `sqrt'". If we use either of these options, we need to link with -lm to resolve the symbol sqrt.

    But when you use a variable for invoking sqrt, gcc does not use any built-in function. It tries to call the function sqrt, which is typically defined in libm.so forcing us to use -lm always.

    Hope this clarifies the issue.
    Ugh... You're responding to a 1.5 years old thread...

    Actually, the thing is that when he compiled with g++, it worked because g++ links your program with libm.so by default. gcc doesn't.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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