Undefined reference?

This is a discussion on Undefined reference? within the C Programming forums, part of the General Programming Boards category; Alright guys, I'm teaching myself C with help from "C for dummies 9 in 1 reference guide" and I'm going ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Location
    Illinois, Usa
    Posts
    3

    Undefined reference?

    Alright guys, I'm teaching myself C with help from "C for dummies 9 in 1 reference guide" and I'm going through and I am stumped on one of the codes the author provided.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        long int hat;
        int loop;
        long int seed;
        
        printf("Enter a seed number:");
        scanf("%d",&seed);
        srandom(seed);
        
        
        for(loop=0;loop<100;loop++)
        {
            hat = rand();
        printf("%5d is a truly random number!\n",hat);
        }
        return(0);
    }
    That is the code that I have typed into Dev-C++ and it's saved as a ".c" but when I compile I get the following errors:

    [Linker error] undefined reference to `random'
    [Linker error] undefined reference to `srandom'

    So my question is why does it pull up this error? How do I go about fixing it? Also why does dev-c++ not like the "random();" function, which is I used "rand();"?

    ~Tirith

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    random() and srandom() are not standard functions.

    rand() and srand() are standard functions.

    I guess your "book" was written by someone who's assuming you're using one particular compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Location
    Illinois, Usa
    Posts
    3
    In the book, it tells me to use dev-C++ which I have dabbled around in. So now my question is why is it still giving me these errors?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    As Salem explains, there ARE no functions named random and srandom!
    You code will NEVER compile, but it's not valid C. So change those names.
    And Dev-C++ is an IDE, not a compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And Dev-C++ is an IDE, not a compiler.
    For the OP's info, the compiler that comes standard with Dev-C++ is MinGW's Windows port of GCC.

    Here's a quick reference I have bookmarked with the standard functions:The C Standard Library. Always be careful to differentiate between what's standard C, and what's specific to your compiler and your operating system.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    60
    nothing much..

    Code:
    #include <times.h>
    
    srand( time( NULL ) );
    srand() not srandom()...
    rand() not random();

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Also, the author lied - those numbers aren't truly random at all ^_^

  8. #8
    Registered User
    Join Date
    Aug 2009
    Location
    Illinois, Usa
    Posts
    3
    Thank you so much for the help and Sean thanks for the link.

    KBriggs, I know, the next exercise introduced seeds.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    As Salem explains, there ARE no functions named random and srandom!
    Yes there are, they are part of the BSD API, which is emulated on lots of other UNIX platforms.

    You code will NEVER compile, but it's not valid C. So change those names.
    It is valid C. It happens to call some functions which are not part of the ISO C standard. If any call to any non-standard function makes your code "invalid" then how can we use libraries? For that matter how can we define our own functions?

    Still, it's strange to see nonstandard calls being touted in a beginner's book.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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