linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* undefined sqrt()
@ 2002-10-24 20:06 Theo. Sean Schulze
  2002-10-24 20:17 ` Karthik Vishwanath
  2002-10-24 20:28 ` Steven Smith
  0 siblings, 2 replies; 9+ messages in thread
From: Theo. Sean Schulze @ 2002-10-24 20:06 UTC (permalink / raw)
  To: linux-c-programming; +Cc: tschulze

Hello,

This is a real newbie type question.  Usually my problems with C involve pointers, but this one is a problem with the function sqrt().  I had a math problem I needed to solve, and having forgotten my high school algebra about 25 years ago, I thought I could quickly write a C program to get an approximate answer by brute force.  This program is supposed to figure out one of the sides of a right triangle using the other side and the hypotenuse.  The hypotenuse has a fixed relationship to the side of unknown lenght.  Anyway, here is the code, such as it is:


//
// Filename: glide.c
// Version: 0.0.1     Date: 24 October, 2002
//
// Author: T. Sean Schulze
// System: i386-slackware-linux-gnu
//
// Purpose: The purpose of this script is to use brute force to find out
// how long the long side and the hypotenuse of a right triangle are 
// when the short side of the triangle is 35 feet long.  This will, of 
// course, vary depending on the angle between the two unknown sides.
//
// Limitations: Down and dirty.  Not useful for much else than this brute
// attempt.

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

double a ;
double b ;
double c ;
double GLIDE ;

int
main(void) /*(int argc, char **argv)*/
{
	GLIDE = 0.012 ;
	a = 0.0 ;

	while( b < 35.0 ) 
	{
		a++ ;
		c = a * (1 + GLIDE) ;
		b = sqrt((c * c) - (a * a)) ;

	}
	
	printf("Glide = %f\n", GLIDE);
	printf("a = %f\n", a) ;
	printf("b = %f\n", b) ;
	printf("c = %f\n", c) ;

	exit(0);

}

When I try to compile it, I get these messages:

dragoon:/home/tschulze/bin # gcc -Wall -o glide glide.c
/tmp/ccXahjXY.o: In function `main':
/tmp/ccXahjXY.o(.text+0x98): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Could someone please give me a hint why this doesn't work?

TIA,
Sean

-- 
Theo. Sean Schulze
tschulze@teamfinders.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-24 20:06 undefined sqrt() Theo. Sean Schulze
@ 2002-10-24 20:17 ` Karthik Vishwanath
  2002-10-24 20:50   ` Theo. Sean Schulze
  2002-10-24 20:28 ` Steven Smith
  1 sibling, 1 reply; 9+ messages in thread
From: Karthik Vishwanath @ 2002-10-24 20:17 UTC (permalink / raw)
  To: Theo. Sean Schulze; +Cc: linux-c-programming

Try:

gcc -Wall -lm -o glide glide.c

-K

----------------------------------------
The biggest difference between time and space is that you 
can't reuse time -- Merrick Furst


On Thu, 24 Oct 2002, Theo. Sean Schulze wrote:

> Hello,
> 
> This is a real newbie type question.  Usually my problems with C involve pointers, but this one is a problem with the function sqrt().  I had a math problem I needed to solve, and having forgotten my high school algebra about 25 years ago, I thought I could quickly write a C program to get an approximate answer by brute force.  This program is supposed to figure out one of the sides of a right triangle using the other side and the hypotenuse.  The hypotenuse has a fixed relationship to the side of unknown lenght.  Anyway, here is the code, such as it is:
> 
> 
> //
> // Filename: glide.c
> // Version: 0.0.1     Date: 24 October, 2002
> //
> // Author: T. Sean Schulze
> // System: i386-slackware-linux-gnu
> //
> // Purpose: The purpose of this script is to use brute force to find out
> // how long the long side and the hypotenuse of a right triangle are 
> // when the short side of the triangle is 35 feet long.  This will, of 
> // course, vary depending on the angle between the two unknown sides.
> //
> // Limitations: Down and dirty.  Not useful for much else than this brute
> // attempt.
> 
> #include <stdio.h>
> #include <math.h>
> #include <float.h>
> #include <stdarg.h>
> #include <stddef.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <errno.h>
> 
> double a ;
> double b ;
> double c ;
> double GLIDE ;
> 
> int
> main(void) /*(int argc, char **argv)*/
> {
> 	GLIDE = 0.012 ;
> 	a = 0.0 ;
> 
> 	while( b < 35.0 ) 
> 	{
> 		a++ ;
> 		c = a * (1 + GLIDE) ;
> 		b = sqrt((c * c) - (a * a)) ;
> 
> 	}
> 	
> 	printf("Glide = %f\n", GLIDE);
> 	printf("a = %f\n", a) ;
> 	printf("b = %f\n", b) ;
> 	printf("c = %f\n", c) ;
> 
> 	exit(0);
> 
> }
> 
> When I try to compile it, I get these messages:
> 
> dragoon:/home/tschulze/bin # gcc -Wall -o glide glide.c
> /tmp/ccXahjXY.o: In function `main':
> /tmp/ccXahjXY.o(.text+0x98): undefined reference to `sqrt'
> collect2: ld returned 1 exit status
> 
> Could someone please give me a hint why this doesn't work?
> 
> TIA,
> Sean
> 
> -- 
> Theo. Sean Schulze
> tschulze@teamfinders.org
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-24 20:06 undefined sqrt() Theo. Sean Schulze
  2002-10-24 20:17 ` Karthik Vishwanath
@ 2002-10-24 20:28 ` Steven Smith
  1 sibling, 0 replies; 9+ messages in thread
From: Steven Smith @ 2002-10-24 20:28 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 443 bytes --]

> When I try to compile it, I get these messages:
> 
> dragoon:/home/tschulze/bin # gcc -Wall -o glide glide.c
> /tmp/ccXahjXY.o: In function `main':
> /tmp/ccXahjXY.o(.text+0x98): undefined reference to `sqrt'
> collect2: ld returned 1 exit status
> 
> Could someone please give me a hint why this doesn't work?
You need to link with the maths library.  Try this:

gcc -Wall -o glide glide.c -lm

Steven Smith,
sos22@cam.ac.uk.

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-24 20:17 ` Karthik Vishwanath
@ 2002-10-24 20:50   ` Theo. Sean Schulze
  2002-10-24 21:50     ` Karthik Vishwanath
  2002-10-25  8:17     ` Glynn Clements
  0 siblings, 2 replies; 9+ messages in thread
From: Theo. Sean Schulze @ 2002-10-24 20:50 UTC (permalink / raw)
  To: linux-c-programming; +Cc: tschulze

Thanks.  The "-lm" did it.  Why?  I see from the gcc man page that the "-l" is a linker option.  Am I right that the linker then searches for "libm.a"?  Again, why, if I am right, would it need to after I included math.h?  Isn't sqrt() in math.h?

Thanks,
Sean

On Thu, Oct 24, 2002 at 04:17:27PM -0400, Karthik Vishwanath hunted and pecked out:
> Try:
> 
> gcc -Wall -lm -o glide glide.c
> 
> -K
> 
> ----------------------------------------
> The biggest difference between time and space is that you 
> can't reuse time -- Merrick Furst
> 
> 
> On Thu, 24 Oct 2002, Theo. Sean Schulze wrote:
> 
> > Hello,
> > 
> > This is a real newbie type question.  Usually my problems with C involve pointers, but this one is a problem with the function sqrt().  I had a math problem I needed to solve, and having forgotten my high school algebra about 25 years ago, I thought I could quickly write a C program to get an approximate answer by brute force.  This program is supposed to figure out one of the sides of a right triangle using the other side and the hypotenuse.  The hypotenuse has a fixed relationship to the side of unknown lenght.  Anyway, here is the code, such as it is:
> > 
> > 
> > //
> > // Filename: glide.c
> > // Version: 0.0.1     Date: 24 October, 2002
> > //
> > // Author: T. Sean Schulze
> > // System: i386-slackware-linux-gnu
> > //
> > // Purpose: The purpose of this script is to use brute force to find out
> > // how long the long side and the hypotenuse of a right triangle are 
> > // when the short side of the triangle is 35 feet long.  This will, of 
> > // course, vary depending on the angle between the two unknown sides.
> > //
> > // Limitations: Down and dirty.  Not useful for much else than this brute
> > // attempt.
> > 
> > #include <stdio.h>
> > #include <math.h>
> > #include <float.h>
> > #include <stdarg.h>
> > #include <stddef.h>
> > #include <stdlib.h>
> > #include <unistd.h>
> > #include <errno.h>
> > 
> > double a ;
> > double b ;
> > double c ;
> > double GLIDE ;
> > 
> > int
> > main(void) /*(int argc, char **argv)*/
> > {
> > 	GLIDE = 0.012 ;
> > 	a = 0.0 ;
> > 
> > 	while( b < 35.0 ) 
> > 	{
> > 		a++ ;
> > 		c = a * (1 + GLIDE) ;
> > 		b = sqrt((c * c) - (a * a)) ;
> > 
> > 	}
> > 	
> > 	printf("Glide = %f\n", GLIDE);
> > 	printf("a = %f\n", a) ;
> > 	printf("b = %f\n", b) ;
> > 	printf("c = %f\n", c) ;
> > 
> > 	exit(0);
> > 
> > }
> > 
> > When I try to compile it, I get these messages:
> > 
> > dragoon:/home/tschulze/bin # gcc -Wall -o glide glide.c
> > /tmp/ccXahjXY.o: In function `main':
> > /tmp/ccXahjXY.o(.text+0x98): undefined reference to `sqrt'
> > collect2: ld returned 1 exit status
> > 
> > Could someone please give me a hint why this doesn't work?
> > 
> > TIA,
> > Sean
> > 
> > -- 
> > Theo. Sean Schulze
> > tschulze@teamfinders.org
> > -
> > To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html
> > 
> 

-- 
Theo. Sean Schulze
tschulze@teamfinders.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-24 20:50   ` Theo. Sean Schulze
@ 2002-10-24 21:50     ` Karthik Vishwanath
  2002-10-25  8:17     ` Glynn Clements
  1 sibling, 0 replies; 9+ messages in thread
From: Karthik Vishwanath @ 2002-10-24 21:50 UTC (permalink / raw)
  To: Theo. Sean Schulze; +Cc: linux-c-programming

from the gcc man page: 

"
        ... The linker handles an archive file by scan-
        ning  through  it  for members which define symbols
        that have so far been referenced but  not  defined.
"

The funtion sqrt() is defined in the libm.a and declared in <math.h>. To
use the function, you need the "real code" (which is in libm.a). Thats the
way I think about it. I'll let the gurus on this list clarify, if anything
is misleading/ misrepresented here. 

-K


On Thu, 24 Oct 2002, Theo. Sean Schulze wrote:

> Thanks.  The "-lm" did it.  Why?  I see from the gcc man page that the
> "-l" is a linker option.  Am I right that the linker then searches for
> "libm.a"?  Again, why, if I am right, would it need to after I
> included math.h?  Isn't sqrt() in math.h?
> 
> Thanks,
> Sean
> 
> On Thu, Oct 24, 2002 at 04:17:27PM -0400, Karthik Vishwanath hunted and pecked out:
> > Try:
> > 
> > gcc -Wall -lm -o glide glide.c
> > 
> > -K
> > 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-24 20:50   ` Theo. Sean Schulze
  2002-10-24 21:50     ` Karthik Vishwanath
@ 2002-10-25  8:17     ` Glynn Clements
  2002-10-25 17:27       ` Theo. Sean Schulze
  1 sibling, 1 reply; 9+ messages in thread
From: Glynn Clements @ 2002-10-25  8:17 UTC (permalink / raw)
  To: Theo. Sean Schulze; +Cc: linux-c-programming


Theo. Sean Schulze wrote:

> Thanks. The "-lm" did it. Why? I see from the gcc man page that the
> "-l" is a linker option. Am I right that the linker then searches for
> "libm.a"?

To be precise, it will normally look for libm.so (shared library)
first (unless the -static switch was used), then look for libm.a.

> Again, why, if I am right, would it need to after I included
> math.h? Isn't sqrt() in math.h?

The declaration is in math.h, but a declaration simply tells the
compiler that the function exists, along with the number and types of
its arguments and its return type.

In order to produce a working program, you have to provide the actual
code which implements that function. In the case of sqrt(), the code
is in libm.

For functions which are in libc, you don't have to provide any -l
switches, as gcc (when used for linking) automatically instructs the
compiler to link against libc.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-25  8:17     ` Glynn Clements
@ 2002-10-25 17:27       ` Theo. Sean Schulze
  2002-10-25 19:29         ` Glynn Clements
  0 siblings, 1 reply; 9+ messages in thread
From: Theo. Sean Schulze @ 2002-10-25 17:27 UTC (permalink / raw)
  To: linux-c-programming

Glynn,

Thanks for the explanation.  I understand now.  After getting your explanation, I did a bit of web browsing to try to find a page that tells me what libraries I need for which header files, but I haven't been too successful.  Do you know of a URL where I could find that info?

Thanks,
Sean

On Fri, Oct 25, 2002 at 09:17:07AM +0100, Glynn Clements hunted and pecked out:
> 
> Theo. Sean Schulze wrote:
> 
> > Thanks. The "-lm" did it. Why? I see from the gcc man page that the
> > "-l" is a linker option. Am I right that the linker then searches for
> > "libm.a"?
> 
> To be precise, it will normally look for libm.so (shared library)
> first (unless the -static switch was used), then look for libm.a.
> 
> > Again, why, if I am right, would it need to after I included
> > math.h? Isn't sqrt() in math.h?
> 
> The declaration is in math.h, but a declaration simply tells the
> compiler that the function exists, along with the number and types of
> its arguments and its return type.
> 
> In order to produce a working program, you have to provide the actual
> code which implements that function. In the case of sqrt(), the code
> is in libm.
> 
> For functions which are in libc, you don't have to provide any -l
> switches, as gcc (when used for linking) automatically instructs the
> compiler to link against libc.
> 
> -- 
> Glynn Clements <glynn.clements@virgin.net>

-- 
Theo. Sean Schulze
tschulze@teamfinders.org

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
  2002-10-25 17:27       ` Theo. Sean Schulze
@ 2002-10-25 19:29         ` Glynn Clements
  0 siblings, 0 replies; 9+ messages in thread
From: Glynn Clements @ 2002-10-25 19:29 UTC (permalink / raw)
  To: Theo. Sean Schulze; +Cc: linux-c-programming


Theo. Sean Schulze wrote:

> Thanks for the explanation. I understand now. After getting your
> explanation, I did a bit of web browsing to try to find a page that
> tells me what libraries I need for which header files, but I haven't
> been too successful. Do you know of a URL where I could find that
> info?

I'm not aware of any site which provides such a listing.

For functions which are part of the ANSI C specification, anything in
math.h is in libm, everything else should be in libc (or, in a few
cases, implemented internally by the compiler).

Non-ANSI Unix functions are normally also in libc, particularly for
systems which use GNU libc (i.e. Linux). For other Unices, "core"
functionality is in libc, while some peripheral functionality (e.g. 
networking APIs) may be in separate libraries; the exact details vary.

For anything else, the documentation for the package which provides
the functionality should state which header files contain the
declarations, and which libraries contain the implementation.

BTW, you can determine the functions which are provided by a given
library using "nm".

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: undefined sqrt()
@ 2002-10-26 10:07 Ruslan U. Zakirov
  0 siblings, 0 replies; 9+ messages in thread
From: Ruslan U. Zakirov @ 2002-10-26 10:07 UTC (permalink / raw)
  To: linux-c-programming

TSS> Glynn,
TSS> Thanks for the explanation.  I understand now.  After getting your explanation, I did a bit of web browsing to try to find a page that tells me what libraries I need for which header files, but
TSS> I haven't been too successful.  Do you know of a URL where I could find that info?
TSS> Thanks,
TSS> Sean
Hello!
There are two sections in man sqrt.
1) Library: In this section described which library you must link with
your program.
2) Sinopsis: Here specified file where you can find prototype of
function. This file(s) you must include in your project.
Good luck.
_______________________________________________________________________
Sorry for my English.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-10-26 10:07 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-24 20:06 undefined sqrt() Theo. Sean Schulze
2002-10-24 20:17 ` Karthik Vishwanath
2002-10-24 20:50   ` Theo. Sean Schulze
2002-10-24 21:50     ` Karthik Vishwanath
2002-10-25  8:17     ` Glynn Clements
2002-10-25 17:27       ` Theo. Sean Schulze
2002-10-25 19:29         ` Glynn Clements
2002-10-24 20:28 ` Steven Smith
  -- strict thread matches above, loose matches on Subject: below --
2002-10-26 10:07 Ruslan U. Zakirov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).