undefined sqrt()
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
Signature · 2 lines
Theo. Sean Schulze [email protected]
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
Quoted 77 lines · Theo. Sean Schulze
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 statusCould someone please give me a hint why this doesn't work?
TIA,
SeanSignature · 6 lines
Theo. Sean Schulze [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
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:
Quoted 91 lines
Try:
gcc -Wall -lm -o glide
glide.c-K
----------------------------------------
The biggest difference between time and space is that you can't reuse time -- Merrick FurstQuoted 77 lines · Theo. Sean Schulze
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 statusCould someone please give me a hint why this doesn't work?
TIA,
SeanSignature · 6 lines
Theo. Sean Schulze [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Theo. Sean Schulze [email protected]
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
Quoted 15 lines · Theo. Sean Schulze
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'tsqrt()inmath.h?Thanks,
SeanOn Thu, Oct 24, 2002 at 04:17:27PM -0400, Karthik Vishwanath hunted and pecked out:
Quoted 6 lines
Try:
gcc -Wall -lm -o glide
glide.c-K
Quoted 3 lines · Theo. Sean Schulze
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.
Quoted 2 lines
Again, why, if I am right, would it need to after I included
math.h? Isn'tsqrt()inmath.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.
Signature · 1 line
Glynn Clements <[email protected]>
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:
Quoted 27 lines
Quoted 3 lines · Theo. Sean Schulze
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.
Quoted 2 lines
Again, why, if I am right, would it need to after I included
math.h? Isn'tsqrt()inmath.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.
Signature · 1 line
Glynn Clements <[email protected]>
Signature · 2 lines
Theo. Sean Schulze [email protected]
Quoted 5 lines · Theo. Sean Schulze
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".
Signature · 1 line
Glynn Clements <[email protected]>
Quoted 8 lines
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 statusCould 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,
[email protected].