Thread: Undefined reference to ln() and ranf()

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Undefined reference to ln() and ranf()

    Hi,

    I am trying to generate gaussian random variables. I am using the following snippet of code to do this. However it gives me the error "undefined reference to ln()". It does the same for ranf(). have included math.h in my header file. It doesnt give me an error for sqrt() because I use the option -lm at command prompt when I compile. This is my first time trying to use these scientific functions. Any help would be appreciated.
    Thanks


    Code:
     for(k=0;k<(N+1);k+2)
        {
          do 
    	{
    	  x1 = 2.0 * ranf() - 1.0;
    	  x2 = 2.0 * ranf() - 1.0;
    	  w = x1 * x2 + x2 * x2;
    	} while(w >= 1.0);
          w = sqrt((-2.0 * ln(w))/w);
          x[k] = x1 * w;
          x[k+1] = x2 * w;
        }
    Last edited by vbdave78; 03-03-2009 at 11:23 AM. Reason: Didnt describe the problem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the error. Note that ranf() and ln() are not part of the C standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Here is a simple program that I ran using the following command
    cc error.c -lm.
    I am running this code on a linux machine.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    main()
    {
      float x1, x2, w, y1, y2; /* Variables used to generate random values */
      int k;
      double x[10];
      int N = 6;
      for(k=0;k<(N+1);k+2)
        {
          do 
    	{
    	  x1 = 2.0 * ranf() - 1.0;
    	  x2 = 2.0 * ranf() - 1.0;
    	  w = x1 * x2 + x2 * x2;
    	} while(w >= 1.0);
          w = sqrt((-2.0 * ln(w))/w);
          x[k] = x1 * w;
          x[k+1] = x2 * w;
          printf("%lf",x[k]);
        }
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where is ranf() and ln() defined? As I mentioned, they are not part of the C standard library (and probably not provided as a compiler extension), so including <math.h> and specifying -lm is of no use except for sqrt().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what normal people call "ln" C calls "log". And if you want ranf, you have to write it yourself, or link in the code that has it in it.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Thank you for your help.
    I have figured out that I need the GSL header files to be able to use those functions. I cant find them on my system anywere however which is a different story all together. But thanks for your input.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Just having the headers isn't going to help, you'll need some type of code library to link to as well (one that goes along with the headers you mention).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Tags for this Thread