 |
BOOK Beginning Linux Programming, 3rd Edition  | This is the forum to discuss the Wrox book Beginning Linux Programming, 2nd Edition by Richard Stones, Neil Matthew, Alan Cox; ISBN: 9780764543739 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK Beginning Linux Programming, 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

September 25th, 2004, 12:15 PM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why are my math functions not being included?
I am using RedHat Linux 7.2 and am working my way through Beginning Linux Programming,3rd ed. I've been encountering errors when attempting to compile program 'limits.c' (pp 167-8). The math function 'log' is not being recognized.
I've tried this out with some of the other math functions which should have been included with 'math.h', and they are missing as well. To demonstrate, suppose I try to compile this simple program:
#include <math.h>
#include <stdio.h>
int main()
{
double x = 4.5;
double y;
y = sqrt(x);
printf("Square root of %.1f is %.3f\n", x, y);
y = log(x);
printf("Natural log of %.1f is %.3f\n", x, y);
y = exp(x);
printf("e raised to power %.1f is %.3f\n", x, y);
exit(0);
}
Compilation fails with the following error message (/tmp info omitted):
In function `main':
undefined reference to `sqrt'
undefined reference to `log'
undefined reference to `exp'
It appears that NONE of the functions which should have been included with 'include <math.h>' were actually included.
I tried this same program on my Mac OS X/Darwin, and the program compiled and ran correctly.
Can anyone tell me why the math.h library is not being included on my Linux installation?
Jim Keenan
Just Another Perl Hacker
|

October 20th, 2004, 05:14 PM
|
Registered User
|
|
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm having the same problem also. Looking at it from a compiling point of view, I would have said the problem lied with glibc, incorrect flags when compiling a possibility.
I'll post back when I've managed to solve it.
|

October 20th, 2004, 06:18 PM
|
Registered User
|
|
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Isn't glibc, here's something I found,
http://www.network-theory.co.uk/docs...cintro_16.html
use '$ gcc -Wall calc.c /usr/lib/libm.a -o calc' while compiling, worked for my perl install. tt
|

October 21st, 2004, 07:55 AM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Harrism: Yes, after posting, I eventually concluded that this was simply a difference in the way the libraries are set up/accessed on Linux and Darwin. As I continued to work through the book, I realized that many of the C programs needed to be compiled in a manner like this:
gcc -o sqrt -I/usr/lib/include sqrt.c -lm
Now, I may have some typos in the above line, because I'm at my day job where I don't have access to Linux, so I'm typing this from memory. But the general idea is correct: After the name of the compiled program, you type a path to the library to be included prefixed by the -I option. Then after the name of the source code file you include the library needed prefixed by the -l (lower case 'l') flag.
When I get to my Linux box I'll try to double-check the typing.
jimk
Just Another Perl Hacker
|

October 22nd, 2004, 05:29 AM
|
Registered User
|
|
Join Date: Jul 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello,
I typed the program and compiled it successfully (in Linux):
$ gcc -o limits limits.c -lm
Ari Constancio
|
|
 |