random() and srandom() unresolved links


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: random() and srandom() unresolved links

  1. #1
    Join Date
    Aug 2004
    Posts
    37

    random() and srandom() unresolved links

    Hi all
    I'm writing a software to be compiled and run on C++ BuilderX. I'm using the standard random generators functions random() and srandom() in my program.here is how I declare these using precompiler directives to support cross-platform behavior:


    Code:
               				// "standard" random number generators
    #ifdef USING_SGI			// SGI versions
    extern "C" long random(void);
    extern "C" void srandom(unsigned);
    #endif
    #ifdef USING_SUNOS5			// SUNOS5 versions
    extern "C" int random(void);
    extern "C" int srandom(unsigned);
    #endif
    #ifdef  USING_CBX
    /*support for CBX platform*/
    
     extern "C"  int random(void);
     extern "C"  void srandom(unsigned);
    #endif
    when i build my program i got theses link errors :

    Code:
    Error: Unresolved external 'srandom(unsigned int)' referenced from C:\CPPBUILDERX\ANN_0.2\WINDOWS\DEBUG_BUILD\RAND.OBJ
    Error: Unresolved external 'random()' referenced from C:\CPPBUILDERX\ANN_0.2\WINDOWS\DEBUG_BUILD\RAND.OBJ
     ILINK32 exited with error code: 2
    Build cancelled due to errors
    I guess the problem is in the prototype declarations :
    Code:
    extern "C"  int random(void);
      extern "C"  void srandom(unsigned);
    I can't figure out what are the prototypes of random() and srandom() in windows Borland c++ Builder X

    any Ideas ?
    please help !

    thanks

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    You're right. These are not the standard functions. The standard ones are random and srand. In C++ Builder they are declared as follows:

    #include <stdlib.h>
    int random(int num);

    srand(unsigned seed);

    I believe that BuilderX uses the same declarations and libraries.

    If that doesn't work, try rand() and srand() respectively:

    //from stdlib.h
    int _RTLENTRY _EXPFUNC rand(void);

    void _RTLENTRY _EXPFUNC srand(unsigned __seed);

    The problem is that random() and srandom() are implemented as macros that wrap rand() and srand() so the linker can't find them.

    BTW, unless you're using a C compiler and a .c file extension, there's no need to wrap these functions with the extern "C" declaration.
    Danny Kalev

  3. #3
    Join Date
    Aug 2004
    Posts
    37
    thanks for replying !
    the problem is that i'm only doing a porting work for a c++ lib that was written for unix system.I have to add windows borland support . so I added the precompiler diretive :
    Code:
    #ifdef  USING_CBX
    /*support for CBX platform*/
    
     extern "C"  int random(void);
     extern "C"  void srandom(unsigned);
    #endif
    can u tell me how can i adjust the above code so the linker could recognize random and srandom ?
    many thnx

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    I think its rand and srand..

    globally search and replace (the best option, in my opinion, because this standardizes the code instead of applying a bandaid)

    or:

    Just do this:
    inline int random ()
    {
    return rand();
    }

    inline void srand(int seed)
    {
    srand(seed);
    }

    or you can #define the names if you prefer
    #define random rand
    #define srandom srand

  5. #5
    Join Date
    Aug 2004
    Posts
    37
    or you can #define the names if you prefer
    #define random rand
    #define srandom srand
    this one works fine !
    my program now can build successfully !
    many thanks to jonnin and for u all guys !

  6. #6
    Join Date
    Aug 2004
    Posts
    37
    Guys I still want your precious help for my ongoing project!

    the compilating of my c++ lib was successful using the borland win32 compiler tools toolsets.

    I'm now attempting to compile it for the Minimalist GNU compiler for windows toolset : It didn't compiled !
    some of the strange errors i got are like :
    Code:
    "stdio.h": C:/CBuilderX/mingw/include/stdio.h type specifier omitted for parameter ` at line 216
    "stdio.h": C:/CBuilderX/mingw/include/stdio.h parse error before `)' token at line 379
    what could be the source of these errors ? how to fix them ?
    many thnx indeed

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    Well, we can't guess what's going on in lines 216 and 379 without seeing them...
    Anyway, try to see if there's a macro masking problem which causes these functions to be replaced by macros. You want to re-read my first post. It explains this potentail problem clealry. Also, make sure that your files have the right extension .
    Danny Kalev

  8. #8
    Join Date
    Aug 2004
    Posts
    37
    I will note here that i had the same problem when i first compiled for the Borland win32 compiler tools ...I adjusted an option about files extensions (i'm using .cc) and it worked...
    I guess that my problem with Min GNU compiler is also about file extensions; but i can't find any option about this under MINGNU compiler options !
    do u have any experience about settin these options for borland c++ builderx?

    thnx

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    File -> New File then in the Create New File Menu choose the .c extension.

    Also make sure that the "Perform C++ compilation regardless of file extension" option is disabled. (It is by default). It's located at:

    Project-> Build Options Explorer Options -> Compile Mode
    Danny Kalev

  10. #10
    Join Date
    Aug 2004
    Posts
    37
    Quote Originally Posted by Danny
    File -> New File then in the Create New File Menu choose the .c extension.

    Also make sure that the "Perform C++ compilation regardless of file extension" option is disabled. (It is by default). It's located at:

    Project-> Build Options Explorer Options -> Compile Mode
    there is no "compile Mode" option for Minimalist GNU compiler toolset !
    borland win32 compiler toolset does have this option !
    I can't figure out the real cause of my problem...what i know is that MINGW compiler is unable to preprocess even the first header file included in my program : "stdio.h" ...
    another question : do i have to install Mingw or is it shiped and installed with CBX distribution?
    any ideas?
    thanks

  11. #11
    Join Date
    Dec 2003
    Posts
    3,366
    I have no idea. Try modern headers <cstdio> and see if that works?

  12. #12
    Join Date
    Nov 2003
    Posts
    4,118
    and btw, it isn't "stdio.h" but <stdio.h>. There is a difference between the two with respect to the default search path. Also, as jonnin said, try <csdtio> instead, unless this is a strictly C program, with the proper file extensions.
    I believe you have to install the MINGW compiler engine. C++ BuilderX supports additional compilers but it doesn't install them for you (which I found very annoying when I reveiwed this product a while ago here on DevX).
    Danny Kalev

  13. #13
    Join Date
    Aug 2004
    Posts
    37
    Quote Originally Posted by Danny
    and btw, it isn't "stdio.h" but <stdio.h>. There is a difference between the two with respect to the default search path. Also, as jonnin said, try <csdtio> instead, unless this is a strictly C program, with the proper file extensions.
    I believe you have to install the MINGW compiler engine. C++ BuilderX supports additional compilers but it doesn't install them for you (which I found very annoying when I reveiwed this product a while ago here on DevX).
    I tried what u suggested but without success :-(
    when i tried mingw compiler toolset with a simple program it worked ; but it doesn't work for my quit large c++ library !
    this stuff is driving me crazzy !

  14. #14
    Join Date
    Aug 2004
    Posts
    37
    Hint :
    why my library compiled with borland win32 compiler toolset but not with mingw toolset ?

  15. #15
    Join Date
    Dec 2003
    Posts
    3,366
    Because the code is fine and the compiler is improperly installed or configured.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center