Thread: log function causing an error

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    9

    log function causing an error

    I am currently working on a program that includes a special exponential function that calls the log function from with in it. (*the code is below). I am getting the following error:

    gcc -c gas.c
    gcc gas.o list.o queue.o -o gas.exe
    Undefined first referenced
    symbol in file
    log gas.o

    which seems to mean that it is not understanding the log function, however I am certain that the math.h library is inclued. Can there be another reason for this error?

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include "bool.h"
    #include "gas.h"

    double exponential(double mean)
    {
    double rnx;
    rnx = drand48();
    return (mean * -1.0 * log(rnx));
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You included math.h, but forgot to link with the math library.
    Code:
    % gcc gas.o list.o queue.o -o gas.exe -lm
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    13
    Also, you have a bug in your code if your variable rnx = 0 then the log function will tend towards infinity, theres an asymptote at log(0), you can think of it being undefined you may want to make sure you don't allow rnx to be 0. This is ofcourse assuming that your random number generator is Gaussian (ie. generating a normal distribution of values between 0.0 and 1.0)
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM