Thread: undefined reference to 'sqrt'

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Angry undefined reference to 'sqrt'

    Help! Where is the mistake ?
    Code:
    double x = 0.0 , y = 0.0, r = x*x + y*y;
    ...
    printf("\n%i	%.2f	%.2f	%.2f\n",i,x,y,sqrt(r));
    ...ore sqrt(x*x + y*y));
    The compiler tell me things like this:
    gmake[2]: *** [c_teilchen_in_kasten] Fehler 1
    gmake[2]: Das Target »all« wurde wegen Fehlern nicht aktualisiert.
    /home/joerg/c_teilchen_in_kasten/src/c_teilchen_in_kasten.c:82: undefined reference to `sqrt'

    Thanks for life out there !

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you have to link to the math library.
    add -lm to the linker flags.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define N 5

    I did !

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No. That's just including the math header.
    I don't know what gmake is or what kind of ide you're using. this -lm flag has to be added either to your projects linker options or to your projects makefile.
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    I'm using Kdevelop under suse linux 10.2

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by joerg View Post
    I'm using Kdevelop under suse linux 10.2
    Guess you have to wait some time. Have just started to install kdevelop. Maybe sombody else can help in the meantime.
    Kurt

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry but it will take a few month until I understand kdevelops project management.
    you could try to compile from the commandline in the meantime
    e.g.
    Code:
    gcc c_teilchen_in_kasten.c -lm
    Kurt

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by joerg View Post
    I'm using Kdevelop under suse linux 10.2
    So? You'll have to tell kdev that you WANT the math lib (-lm).

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Thank you Kurt !

    It's a mess.
    you wright a couple of words for source code, therefore you have to go through a jungle of s...
    and Kdelvelop creates an entire PROJEKT !

    Joerg

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For simple projects, certainly those in a single file, compiling from the command line is easiest.
    Plus you get to learn about all the things like include and library paths etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM