Question about compiling files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bowlderyu

    Question about compiling files

    Hello, all.
    I meet a question about compiling files.
    Eg., I have three files named myfile.h, myfile.c and the main.c file,
    and there is a function, ex, void myfun(...).

    If I put myfun(...) in the main.c file like the following,

    .....
    void myfun(...)
    {
    .....
    }
    int main(void)
    {
    ....
    }

    it works well.

    But when I move myfun(...) to the file of myfile.c, and a make error
    message appears when the same command "make" runs, although I have
    declear the myfun(...) function in the main function.

    The message seems that the myfun(...) is not decleared before used.

    And this is my Makefile:
    #test Makefile
    objects=main.o
    test:$(objects)
    cc -o test $(objects) -lgsl -lgslcblas -lm
    ..PHONY:clean
    clean:
    rm test $(objects)

    My code may be not very clear, so I don't attach it here.

    I just move a function from one file to another, and give some
    declearation in the header and main files. But it cann't work.

    It will be appreciate for you help.

    Thanks.

  • James Kuyper

    #2
    Re: Question about compiling files

    bowlderyu wrote:
    Hello, all.
    I meet a question about compiling files.
    Eg., I have three files named myfile.h, myfile.c and the main.c file,
    and there is a function, ex, void myfun(...).
    >
    If I put myfun(...) in the main.c file like the following,
    >
    ....
    void myfun(...)
    {
    ....
    }
    int main(void)
    {
    ...
    }
    >
    it works well.
    >
    But when I move myfun(...) to the file of myfile.c, and a make error
    message appears when the same command "make" runs, although I have
    declear the myfun(...) function in the main function.
    >
    The message seems that the myfun(...) is not decleared before used.
    >
    And this is my Makefile:
    #test Makefile
    objects=main.o
    test:$(objects)
    cc -o test $(objects) -lgsl -lgslcblas -lm
    .PHONY:clean
    clean:
    rm test $(objects)
    >
    My code may be not very clear, so I don't attach it here.
    That decision makes it pretty nearly impossible to help you. Your code
    apparently has a mistake, but without seeing the code, we can't make any
    guesses about what the mistake is. Well, that's not quite true - it
    seems virtually certain that there's a problem with your declaration of
    the function in main.c; but without seeing your code, we have no idea
    what that problem is.

    Simplify the code as much as possible, while still showing the problem.
    The show us the full text of your simplified program. Don't remove
    anything from the final program when you show it to us. You have a
    problem that you don't understand - therefore, it's a virtual certainty
    that whatever the problem with your code actually is, it will be one of
    the things you decide to cut out, because you incorrectly think that it
    isn't relevant.

    At a minimum, your code needs the definition of main(), including the
    declaration of myfun() and the call to myfun(). Also, please provide the
    full text of the error messages produced when you tried to compile it.

    Comment

    • Malcolm McLean

      #3
      Re: Question about compiling files

      "bowlderyu" <bowlderyu@gmai l.comwrote
      I meet a question about compiling files.
      Eg., I have three files named myfile.h, myfile.c and the main.c file,
      and there is a function, ex, void myfun(...).
      >
      If I put myfun(...) in the main.c file like the following,
      >
      ....
      void myfun(...)
      {
      ....
      }
      int main(void)
      {
      ...
      }
      >
      it works well.
      >
      But when I move myfun(...) to the file of myfile.c, and a make error
      message appears when the same command "make" runs, although I have
      declear the myfun(...) function in the main function.
      >
      The message seems that the myfun(...) is not decleared before used.
      >
      And this is my Makefile:
      #test Makefile
      objects=main.o
      test:$(objects)
      cc -o test $(objects) -lgsl -lgslcblas -lm
      .PHONY:clean
      clean:
      rm test $(objects)
      >
      Try compiling with the command

      cc *.c -lgsl -lgslcblas -lm

      I'm not saying don't use make. However to troubleshoot, get makefile
      glitches out of the equation.

      Now make sure that a body of every function is provided, and a prototype for
      every non-static function is in the matchign header, and that headers are
      included for each calling function.

      If that doesn't fix your problem then comment out all your code out (use #if
      0) and replace main with a single call to printf(). Then comment things back
      in again until you find the minimum comment in that will cause the problem
      to reappear.

      --
      Free games and programming goodies.




      Comment

      • Richard Heathfield

        #4
        Re: Question about compiling files

        bowlderyu said:
        Hello, all.
        I meet a question about compiling files.
        Eg., I have three files named myfile.h, myfile.c and the main.c file,
        and there is a function, ex, void myfun(...).
        >
        If I put myfun(...) in the main.c file like the following,
        >
        ....
        void myfun(...)
        {
        ....
        }
        int main(void)
        {
        ...
        }
        >
        it works well.
        >
        But when I move myfun(...) to the file of myfile.c,
        /* myfile.h */
        #ifndef H_MYFILE_H
        #define H_MYFILE_H 1
        void myfun(void);
        #endif

        /* myfile.c */
        #include "myfile.h"
        #include <stdio.h>
        void myfun(void)
        {
        puts("Hello, world!");
        }

        /* main.c */
        #include "myfile.h"
        int main(void)
        {
        myfun();
        return 0;
        }

        <snip>
        My code may be not very clear,
        It's well worth writing clear code, if only so that it makes it easier for
        others to help you.

        --
        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

        • Barry Schwarz

          #5
          Re: Question about compiling files

          On Mon, 06 Oct 2008 00:20:39 +0800, bowlderyu <bowlderyu@gmai l.com>
          wrote:
          >Hello, all.
          >I meet a question about compiling files.
          >Eg., I have three files named myfile.h, myfile.c and the main.c file,
          >and there is a function, ex, void myfun(...).
          >
          >If I put myfun(...) in the main.c file like the following,
          >
          >....
          >void myfun(...)
          >{
          >....
          >}
          >int main(void)
          >{
          >...
          >}
          >
          >it works well.
          >
          >But when I move myfun(...) to the file of myfile.c, and a make error
          >message appears when the same command "make" runs, although I have
          >declear the myfun(...) function in the main function.
          If you move the definition of myfun to a different c file, HOW do you
          declare it before it is used?
          >
          >The message seems that the myfun(...) is not decleared before used.
          Messages don't seem anything. Or what they seem to you may not be
          what they seem to someone else. Show us the exact text of the error
          message, preferably by cut and paste, not retyping. Is the message
          generated in the compile phase or the link phase? If during the
          compile phase, you are also going to have to show us the relevant
          parts of the code (at a minimum, the #include directives, all function
          definition signatures, most declarations, and several lines leading up
          to the line in question).
          >
          >And this is my Makefile:
          >#test Makefile
          >objects=main .o
          >test:$(objects )
          > cc -o test $(objects) -lgsl -lgslcblas -lm
          >.PHONY:clean
          >clean:
          > rm test $(objects)
          Make files are off-topic here because they are not part of the
          language and differ from system to system. You would get better
          advice in a group that discusses your system.

          However, when you move the function to a different .c file, you now
          have two .c files to compile. That means you should have two .o files
          to link. How does your make file know about the second one?
          >
          >My code may be not very clear, so I don't attach it here.
          >
          >I just move a function from one file to another, and give some
          >declearation in the header and main files. But it cann't work.
          Why do you duplicate declarations in a .h and a .c file?

          --
          Remove del for email

          Comment

          • bowlderyu

            #6
            Re: Question about compiling files

            Richard Heathfield <rjh@see.sig.in validwrites:
            bowlderyu said:
            >
            >Hello, all.
            >I meet a question about compiling files.
            >Eg., I have three files named myfile.h, myfile.c and the main.c file,
            >and there is a function, ex, void myfun(...).
            >>
            >If I put myfun(...) in the main.c file like the following,
            >>
            >....
            >void myfun(...)
            >{
            >....
            >}
            >int main(void)
            >{
            >...
            >}
            >>
            >it works well.
            >>
            >But when I move myfun(...) to the file of myfile.c,
            >
            /* myfile.h */
            #ifndef H_MYFILE_H
            #define H_MYFILE_H 1
            void myfun(void);
            #endif
            >
            /* myfile.c */
            #include "myfile.h"
            #include <stdio.h>
            void myfun(void)
            {
            puts("Hello, world!");
            }
            >
            /* main.c */
            #include "myfile.h"
            int main(void)
            {
            myfun();
            return 0;
            }
            >
            Thank you for your suggestion.
            I reedit the files as above, but the same message apears.
            And I comment where myfun() is used in the main.c file, it can be
            compiled.

            It is surprised to me when I remove the comment above, it works well!

            I mean, I just add comment, compile; remove comment, compile. It can
            work.

            I don't know the reason. Maybe some files need to update?

            Anyway, thanks all of you.
            <snip>
            >
            >My code may be not very clear,
            >
            It's well worth writing clear code, if only so that it makes it easier for
            others to help you.
            Good suggestion.



            Comment

            • Richard Heathfield

              #7
              Re: Question about compiling files

              bowlderyu said:
              Richard Heathfield <rjh@see.sig.in validwrites:
              >
              >bowlderyu said:
              >>
              >>Hello, all.
              >>I meet a question about compiling files.
              >>Eg., I have three files named myfile.h, myfile.c and the main.c file,
              >>and there is a function, ex, void myfun(...).
              >>>
              >>If I put myfun(...) in the main.c file like the following,
              >>>
              >>....
              >>void myfun(...)
              >>{
              >>....
              >>}
              >>int main(void)
              >>{
              >>...
              >>}
              >>>
              >>it works well.
              >>>
              >>But when I move myfun(...) to the file of myfile.c,
              >>
              <multiple file demo snipped>
              >>
              Thank you for your suggestion.
              I reedit the files as above, but the same message apears.
              Then it's probably because your Makefile is screwed.

              With GNU tools, the simplest way to write a Makefile to build my example
              would be as follows (with [TAB] representing a hard tab character):

              WARN = -W -Wall -ansi -pedantic
              CC = gcc
              myprog: main.o myfile.o
              [TAB]$(CC) $(WARN) -o myprog main.o myfile.o
              main.o: main.c
              [TAB]$(CC) $(WARN) -c -o main.o main.c
              myfile.o: myfile.c
              [TAB]$(CC) $(WARN) -c -o myfile.o myfile.c

              --
              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

              • lovecreatesbeauty@gmail.com

                #8
                Re: Question about compiling files

                On Oct 6, 8:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                With GNU tools, the simplest way to write a Makefile to build my example
                would be as follows (with [TAB] representing a hard tab character):
                >
                WARN = -W -Wall -ansi -pedantic
                CC = gcc
                myprog: main.o myfile.o
                [TAB]$(CC) $(WARN) -o myprog main.o myfile.o
                main.o: main.c
                [TAB]$(CC) $(WARN) -c -o main.o main.c
                myfile.o: myfile.c
                [TAB]$(CC) $(WARN) -c -o myfile.o myfile.c
                >
                I think this is simpler Makefile

                CC = gcc
                CFLAGS = -ansi -pedantic -Wall -W
                LDFLAGS =
                OBJS = a.o b.o
                OUT = a.out

                $(OUT) : $(OBJS)
                $(CC) $(LDFLAGS) $^ -o $@

                a.o b.o : b.h

                ..PHONY : clean
                clean :
                rm $(OUT) $(OBJS)

                Comment

                • Richard Heathfield

                  #9
                  Re: Question about compiling files

                  lovecreatesbeau ty@gmail.com said:
                  On Oct 6, 8:16 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  >With GNU tools, the simplest way to write a Makefile to build my example
                  >would be as follows (with [TAB] representing a hard tab character):
                  >>
                  >WARN = -W -Wall -ansi -pedantic
                  >CC = gcc
                  >myprog: main.o myfile.o
                  >[TAB]$(CC) $(WARN) -o myprog main.o myfile.o
                  >main.o: main.c
                  >[TAB]$(CC) $(WARN) -c -o main.o main.c
                  >myfile.o: myfile.c
                  >[TAB]$(CC) $(WARN) -c -o myfile.o myfile.c
                  >>
                  >
                  I think this is simpler Makefile
                  >
                  CC = gcc
                  CFLAGS = -ansi -pedantic -Wall -W
                  LDFLAGS =
                  OBJS = a.o b.o
                  OUT = a.out
                  >
                  $(OUT) : $(OBJS)
                  $(CC) $(LDFLAGS) $^ -o $@
                  >
                  a.o b.o : b.h
                  >
                  .PHONY : clean
                  clean :
                  rm $(OUT) $(OBJS)
                  It's more complex (because it uses more facts about makefiles). It's also
                  longer. I don't dispute that it has advantages over mine, but simplicity
                  for the building of tiny little programs is not one of them.


                  --
                  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

                  • bowlderyu

                    #10
                    Re: Question about compiling files

                    Richard Heathfield <rjh@see.sig.in validwrites:
                    bowlderyu said:
                    >
                    >Richard Heathfield <rjh@see.sig.in validwrites:
                    >>
                    >>bowlderyu said:
                    >>>
                    >>>Hello, all.
                    >>>I meet a question about compiling files.
                    >>>Eg., I have three files named myfile.h, myfile.c and the main.c file,
                    >>>and there is a function, ex, void myfun(...).
                    >>>>
                    >>>If I put myfun(...) in the main.c file like the following,
                    >>>>
                    >>>....
                    >>>void myfun(...)
                    >>>{
                    >>>....
                    >>>}
                    >>>int main(void)
                    >>>{
                    >>>...
                    >>>}
                    >>>>
                    >>>it works well.
                    >>>>
                    >>>But when I move myfun(...) to the file of myfile.c,
                    >>>
                    <multiple file demo snipped>
                    >>>
                    >Thank you for your suggestion.
                    >I reedit the files as above, but the same message apears.
                    >
                    Then it's probably because your Makefile is screwed.
                    >
                    With GNU tools, the simplest way to write a Makefile to build my example
                    would be as follows (with [TAB] representing a hard tab character):
                    >
                    WARN = -W -Wall -ansi -pedantic
                    CC = gcc
                    myprog: main.o myfile.o
                    [TAB]$(CC) $(WARN) -o myprog main.o myfile.o
                    main.o: main.c
                    [TAB]$(CC) $(WARN) -c -o main.o main.c
                    myfile.o: myfile.c
                    [TAB]$(CC) $(WARN) -c -o myfile.o myfile.c
                    Now, the above code is a little hard for me to understand in detail.

                    I need work hard for it.

                    Regards,

                    bowlderyu

                    Comment

                    Working...