p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK Beginning Linux Programming, 3rd Edition (http://p2p.wrox.com/forumdisplay.php?f=120)
-   -   Why are my math functions not being included? (http://p2p.wrox.com/showthread.php?t=18971)

jkeen September 25th, 2004 12:15 PM

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

Harrism October 20th, 2004 05:14 PM

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.


Harrism October 20th, 2004 06:18 PM

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



jkeen October 21st, 2004 07:55 AM

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

AriConstancio October 22nd, 2004 05:29 AM

Hello,

I typed the program and compiled it successfully (in Linux):
$ gcc -o limits limits.c -lm

Ari Constancio



All times are GMT -4. The time now is 04:32 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.