Link through Header Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rayuthar@gmail.com

    Link through Header Files

    Hi,

    I am having three files similar to the stucture given below, while i
    execute my test.c, it show error /test.c:5: undefined reference to
    `myfunction'.
    I have included myprogram.h in both the files myprogram.c and test.c.

    if i comment out the line myfunction from myfunction.h, then it is
    working properly. Is it multiple definition error?
    i compile the files as
    cc -c myprogram.c -o myprogram
    cc -c test.c -o test
    cc -o test test.o my

    Thanks in Advance!


    /******* myprogram.c *******/

    #include "myprogram. h"
    myfunction()
    {
    }


    /******* myprogram.h *******/

    myfunction();


    /******** test.c *********/
    #include "myprogram. h"
    int main()
    {
    myfunction();
    }
  • Richard Heathfield

    #2
    Re: Link through Header Files

    rayuthar@gmail. com said:
    Hi,
    >
    I am having three files similar to the stucture given below, while i
    execute my test.c, it show error /test.c:5: undefined reference to
    `myfunction'.
    >
    I have included myprogram.h in both the files myprogram.c and test.c.
    >
    if i comment out the line myfunction from myfunction.h, then it is
    working properly. Is it multiple definition error?
    No - your code has some issues, but it isn't the showstopper in this case.
    Rather, it's the way you're invoking the linker.
    i compile the files as
    cc -c myprogram.c -o myprogram
    cc -c test.c -o test
    cc -o test test.o my
    This can't work, for any reasonable definition of "work".

    I suggest you change it, for the time being, to:

    cc -c myprogram.c myprogram.o
    cc -c test.c -o test.o
    cc -o myprogram test.o myprogram.o

    <snip>

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Ian Collins

      #3
      Re: Link through Header Files

      rayuthar@gmail. com wrote:
      Hi,
      >
      I am having three files similar to the stucture given below, while i
      execute my test.c, it show error /test.c:5: undefined reference to
      `myfunction'.
      I have included myprogram.h in both the files myprogram.c and test.c.
      >
      if i comment out the line myfunction from myfunction.h, then it is
      working properly. Is it multiple definition error?
      i compile the files as
      cc -c myprogram.c -o myprogram
      cc -c test.c -o test
      cc -o test test.o my
      >
      Where's myprogram in the line above?
      >
      /******* myprogram.h *******/
      >
      myfunction();
      >
      Don't use implicit it return, please.



      --
      Ian Collins.

      Comment

      • Keith Thompson

        #4
        Re: Link through Header Files

        Richard Heathfield <rjh@see.sig.in validwrites:[...]
        >i compile the files as
        >cc -c myprogram.c -o myprogram
        >cc -c test.c -o test
        >cc -o test test.o my
        >
        This can't work, for any reasonable definition of "work".
        >
        I suggest you change it, for the time being, to:
        >
        cc -c myprogram.c myprogram.o
        cc -c test.c -o test.o
        cc -o myprogram test.o myprogram.o
        You missed the "-o" on the first line.

        For that matter, the first two commands can be simplified by using the
        default output file name:

        cc -c myprogram.c
        cc -c test.c

        though the "-o" does show more explicitly what's going on.

        Note that this is about a particular implementation, not about C.
        For more details, consult your system's documentation for the "cc"
        command, or ask in comp.unix.progr ammer.

        --
        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Richard Heathfield

          #5
          Re: Link through Header Files

          Keith Thompson said:

          <snip>
          You missed the "-o" on the first line.
          So I did.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • rayuthar@gmail.com

            #6
            Re: Link through Header Files

            On Apr 14, 11:04 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
            Keith Thompson said:
            >
            <snip>
            >
            You missed the "-o" on the first line.
            >
            So I did.
            >
            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999
            Thanks to everyone! But i found out the problem and fault was mine. i
            was trying to invoke a static function from another c file!

            Comment

            Working...