Undefined reference to ln() and ranf()

This is a discussion on Undefined reference to ln() and ranf() within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to generate gaussian random variables. I am using the following snippet of code to do this. ...

  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
    22,276
    Post the smallest and simplest program that demonstrates the error. Note that ranf() and ln() are not part of the C standard library.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,276
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,806
    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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21