why doesn't the compiler recognize the function although I included the right libs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • David727
    New Member
    • Jul 2010
    • 10

    why doesn't the compiler recognize the function although I included the right libs?

    this is the code
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void main () {
    int b ;
    randomize () ;
    b = random (61) + 40 ;
    printf ("This is b: %d\n", b ) ;
    }
    These are the errors:

    error LNK2019:unresol ved external symbol_random referenced in function_main

    error LNK2019:unresol ved external symbol_randomiz e referenced in function_main
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You haven't included the right libs at all because libs aren't included they are linked to.

    random and randomize are not standard library functions but assuming that they extensions for the compiler you are using and declared in one of the standard headers you have included all you have done is tell the compiler that the functions exist somewhere, which is enough for the compiler to compile.

    However when the linker runs it tries to actually find the functions in one of the supplied object or library files and when it fails it emits LNK2019.

    That suggests you haven't included the correct object or library into your link.

    Comment

    • David727
      New Member
      • Jul 2010
      • 10

      #3
      Thanks for the answer Banfa but
      1.By your explanation I should pass the compiler phase--and I didn't. The fail wasn't in the Run phase but in the compiler phase.
      2. As much as I know, random function belongs to stdlib library. If not...Do you know about another library it belongs to?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No you did pass the compiler phase, you error starts with LNK indicating it is in the link phase of creating the program.

        If it was a compiler error it would have started C....

        You compile every c/cpp file in your program and then link all the objects together with any libraries you need using the linker. That produces an executable that you can run.

        Comment

        • David727
          New Member
          • Jul 2010
          • 10

          #5
          When I try to run it, I fail and get a message error that "The system cannot finf the file specified".

          Allright then, I wrote now all the errors I got (not only the 2 I wrote before).

          error LNK1120: 2 unresolved externals

          error LNK2019:unresol ved external symbol_random referenced in function_main

          error LNK2019:unresol ved external symbol_randomiz e referenced in function_main

          intelliSense: identifier "random" is undefined.
          IntelliSense: identifier "randomize" is undefined


          May you tell me what should I do for using the function "random"? and in general, What should I do for including libraries and using all the functions inside them?

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            What led you to believe that functions random and randomize exist, and that their prototypes are as shown below?
            Code:
            void randomize(void);
            int random(int);
            Where ever you found about about these functions should also have specified the header file to #include in your source file and the library to specify on your command line or in your IDE.

            Comment

            • David727
              New Member
              • Jul 2010
              • 10

              #7
              Ok friends, I found it: The correct names are :rand(int) and srand().
              Thank you.

              Comment

              Working...