Wrox Programmer Forums Why are my math functions not being included?
 Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 Read more about Beginning Linux Programming, 2nd Edition or buy the book from your favorite retailer Download the code for Beginning Linux Programming, 2nd Edition
 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
September 25th, 2004, 12:15 PM
 Registered User Join Date: Sep 2004 Location: Brooklyn, NY, USA. 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 Location: , , . 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 Location: , , . 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 Location: Brooklyn, NY, USA. 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 Location: , , . 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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Included XSLT and use as object YoungLuke C# 2 October 31st, 2007 04:40 AM Math Functions on dynamic data jroxit Classic ASP Basics 3 January 23rd, 2007 12:07 PM Getting percentages with math functions tslag XSLT 1 June 22nd, 2006 11:47 AM Graphics Math Functions vb certified Pro VB.NET 2002/2003 2 January 1st, 2006 11:06 AM Math functions-Please help! jroxit ASP.NET 2.0 Basics 5 December 28th, 2005 12:21 PM

All times are GMT -4. The time now is 03:25 PM.