Linker problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shana
    New Member
    • Oct 2006
    • 20

    Linker problem

    I have created one header file calc.h and had saved it in INCLUDE directory that has following declaration:
    #define NUMBER '0'
    void push(double)
    double pop(void)
    int getop(char[])
    int getch(void)
    void ungetch(int)

    And have created getop.c , stack.c , getch.c and saved in LIB directory.

    In stack.c, pop and push has been defined and getch.c, getch() and ungetch() code is written.

    I have created main.c program and has included the header file calc.h.
    Compilation is not giving error but on executing it is giving linker error that _getop(), _pop() and _push() are undefined symbols.

    PLZZZ i neeeed help to sort out this messss. I even try to save in BIN directory all the programs but does not help and I am not very aware of project creation in C++ right now.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    OK having functions called getch and ungetch is a bad idea because the C standard library (and yess C++ uses it too) defines functions with this name which is probably why you are getting link errors for those functions.

    I would guess that you are doing something like this

    cc main.c

    In most compilers this will compile main.c to main.obj and then try to link main.obj into a program. However main.c does not contain all your code.

    getop.c stack.c getch.c should not be in the lib directory, they are not part of any standard library they are part of your program code, they should be in the program source directory along with main.c.

    Then you will be able to perform the following command

    cc main.c getop.c stack.c getch.c

    to compile and link all your code.

    Comment

    • Shana
      New Member
      • Oct 2006
      • 20

      #3
      Originally posted by Banfa
      OK having functions called getch and ungetch is a bad idea because the C standard library (and yess C++ uses it too) defines functions with this name which is probably why you are getting link errors for those functions.

      I would guess that you are doing something like this

      cc main.c

      In most compilers this will compile main.c to main.obj and then try to link main.obj into a program. However main.c does not contain all your code.

      getop.c stack.c getch.c should not be in the lib directory, they are not part of any standard library they are part of your program code, they should be in the program source directory along with main.c.

      Then you will be able to perform the following command

      cc main.c getop.c stack.c getch.c

      to compile and link all your code.

      Actually I am trying to see the how functions outside the source program can be accessed be including header file created which has those function's declaration. Those functions should be defined some where else. I think This is how prinft scanf works!!!

      If I have to include evry thing in the main.c program itself then wat is the use of creating my own header file? Plzzz I really need to know, I hav literally crushed my brain to make it work ......!!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by Shana
        I think This is how prinft scanf works!!!
        Well you are wrong. You may not find the source code to printf and scanf (it isn't required but some toolsets ship it) on your system because they have already been compiled into the C runtime library. This is a library of binary objects.

        You however have created 4 c files which call code in each other but are expecting the program to work by only compiling 1 of them.

        If you have written the code it is probably worth compiling it so you need to compile and link all 4 files.

        You could initially compile and link 3 of the files excluding main into your on private library and the compile main and link it with the library.

        You do not have to include all the code in main.c but you do have to compile and link all your code files.

        Comment

        • Shana
          New Member
          • Oct 2006
          • 20

          #5
          Originally posted by Banfa
          Well you are wrong. You may not find the source code to printf and scanf (it isn't required but some toolsets ship it) on your system because they have already been compiled into the C runtime library. This is a library of binary objects.

          You however have created 4 c files which call code in each other but are expecting the program to work by only compiling 1 of them.

          If you have written the code it is probably worth compiling it so you need to compile and link all 4 files.

          You could initially compile and link 3 of the files excluding main into your on private library and the compile main and link it with the library.

          You do not have to include all the code in main.c but you do have to compile and link all your code files.
          Thanks for knowledge about printf and scanf. I have complied all files separately and is not giving me any error but yes when I try to run main.c file not compile as compilation is not giving me error, it is giving linker error. I can send u an entire program code as an attachment if u allow me to send u message.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Don't send the code, post the errors and the command lines you are using.

            Comment

            Working...