Undefined reference to 'sqrt'

This is a discussion on Undefined reference to 'sqrt' within the C Programming forums, part of the General Programming Boards category; -lm lib was indicated at the start of the project. Im using the math.h include also. So why do I ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Undefined reference to 'sqrt'

    -lm lib was indicated at the start of the project.

    Im using the math.h include also.

    So why do I keep getting it when using sqrt?

    Kdevelop btw.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is right there.
    No, there. That's it.


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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Im sorry?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It may well be how you specified -lm. I don't use kdevelop so I can't say.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    When linking on UNIX, if a code module A depends on code module B, then module B must be listed after module A on the link line.

    This is wrong:

    Code:
    gcc -lm -o myprogram myprogram.c
    This is right:

    Code:
    gcc -o myprogram myprogram.c -lm
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, I was just using my psychic powers to spot the issue. I assumed you'd use yours to see where I found the problem. Or you could search the forums and see what everyone else did. Or you could just compile it via command line:

    gcc -o foo foo.c -lm


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

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    If a bit of thinking would solve it I would not browse the web trying to get an answer and ultimatly posting.

    Im quite new to this and so I asked, thanks for the replies though, I can understand that every other thread is made by someone with a bit more knowledge so you're used to that :P

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    32
    While I can't presume to speak for quzah, I'm assuming he was just referring to your original post when you said:

    -lm lib was indicated at the start of the project
    That jumped out at me right away before even reading the replies. So it almost seemed like you answered your own question right there. But I can certainly understand the whole deal... hey we've all been there.

    Has nobody mentioned circular references yet? I haven't had to deal with those since my days on the DEC Alpha... either my code has gotten better or the linker has gotten smarter.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by core_cpp View Post
    Has nobody mentioned circular references yet? I haven't had to deal with those since my days on the DEC Alpha... either my code has gotten better or the linker has gotten smarter.
    Yes, I've had problems with circular references before. Usually it means that two libraries which are separate are actually just two parts of a single library and should be merged. But in some cases the code is broken into libraries only for reasons of convenience. In either case the circular references are painful, but can always be resolved by:

    Code:
    -lA -lB -lA
    Where A and B are mutually dependent libraries.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21