Thread: sqrt() function help

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    sqrt() function help

    hey, i'm curious as to how you would go about passing a variable to the sqrt() function to get the square root of it. i've found that you can directly input a number to it, but when i try something like:
    Code:
    square_root = sqrt(user_number);
    i get:
    [tim@shibby documents]$ gcc -o SqRt sqrt.c
    /tmp/ccuCIkhZ.o(.text+0x82): In function `main':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    so how can i do this? is there a better way? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by willc0de4food
    hey, i'm curious as to how you would go about passing a variable to the sqrt() function to get the square root of it. i've found that you can directly input a number to it, but when i try something like:
    Code:
    square_root = sqrt(user_number);
    i get:
    [tim@shibby documents]$ gcc -o SqRt sqrt.c
    /tmp/ccuCIkhZ.o(.text+0x82): In function `main':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status

    so how can i do this? is there a better way? thanks

    Oki as I dont have your full code I will try to figure out what is wrong. Are you incluiding the #<math.h> header?

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Without the full code I can't know, but I belive that error would most likely come from the lack of the math.h header.
    Add
    #include <math.h>
    to the top of your code if you don't have it already

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    You have to link with the math library.
    gcc -o blah blah.c -lm
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    yea, i included the math.h header and i also tried including the tgmath.h header b/c i was unsure about which one to include and neither worked.

    but chrismiceli adding -lm worked thanks. but can you tell me what that does?
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by willc0de4food
    yea, i included the math.h header and i also tried including the tgmath.h header b/c i was unsure about which one to include and neither worked.

    but chrismiceli adding -lm worked thanks. but can you tell me what that does?
    It is linking the library, take a look here -> http://developer.apple.com/documenta...libtool_3.html
    maybe no the best site for the topic but surely explains what you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Sqrt() Function
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 04-22-2003, 04:31 PM