Menu

newbie question about makefile

2005-08-21
2012-09-26
  • Nobody/Anonymous

    I'm reading a tutorial (http://computer.howstuffworks.com/c.htm/)

    and I came to the following instructions

    gcc -c -g util.c

    gcc -c -g main.c

    gcc -o main main.o util.o

    Since I have gcc.exe in C:\Dev-Cpp\bin while I have util.c, main.c in C:\Dev-Cpp\bin\htw the above didn't work until I changed it to

    gcc -c -g htw\util.c -o htw\util.o

    gcc -c -g htw\main.c -o htw\main.o

    gcc -o htw\main.exe htw\main.o util.o

    Then it went OK and produced util.o, main.o and main.exe in the htw directory.

    Now the next instruction was to create the corresponding makefile file. The instructions are as follows:

    "Makefiles
    It can be cumbersome to type all of the gcc lines over and over again, especially if you are making a lot of changes to the code and it has several libraries. The make facility solves this problem. You can use the following makefile to replace the compilation sequence above:

    main: main.o util.o
    gcc -o main main.o util.o
    main.o: main.c util.h
    gcc -c -g main.c
    util.o: util.c util.h
    gcc -c -g util.c

    Enter this into a file named makefile, and type make to build the executable. Note that you must precede all gcc lines with a tab. "

    So, I entered the text

    main: main.o util.o
    gcc -o main main.o util.o
    main.o: main.c util.h
    gcc -c -g main.c
    util.o: util.c util.h
    gcc -c -g util.c

    into a file which I called Makefile.win which I saved into the htw folder.

    Then I tried to execute it with make.exe (which is inside bin)
    C:\Dev-Cpp\bin>make Makefile.win

    But got the following message

    make: *** No rule to make target 'Makefile.win'. Stop

    Can anyone tell me what I'm doing wrong and how I should proceed instead.

    I know you can use Dev-C project to make this things for you but I'm also trying to learn gcc which Dev-C uses so that I can get better feeling for what's happening.

     
    • Paul van Zelst

      Paul van Zelst - 2005-08-21

      make automatically uses a file called "Makefile" in the current directory as its input file. Any command line arguments you pass are interpreted as target names (if they're not valid command line options). So, the command "make Makefile.win" instructs make to look for a target named "Makefile.win" in the file "Makefile" in the current directory. This isn't what you want.
      In order for make to use a different input file, you need to specify it with the -f option.
      So, your command becomes:

      make -f Makefile.win

      Paul

       
      • Nobody/Anonymous

        strange, I tried that with msys and a devcpp created makefile but I get the following error:

        unknown@OEMI /c/cpp_projects/tabcontrol
        $ make -f Makefile.win
        C:\C-COMPILE\1.0\BIN\make.exe: *** No targets specified and no makefile found. Stop.

        if I remove the .win ending it works well...

         
      • Nobody/Anonymous

        bump

         
        • Wayne Keen

          Wayne Keen - 2005-09-01

          Note that since this really is not an MSYS forum, you might do better posting to their mailing list.

          Wayne

           
          • Nobody/Anonymous

            annoyed of my question Wayne? :o(

            since dev-cpp comes with mingw/gcc and there are loads of experienced programmers in here, imo the chances are good that someone might know the reason why this didn't work for me.
            Thanks for your suggestion, but I'm not going to search for a mailing list to get an idea why it doesn't work. I'm going to take into account that it might be a MSYS related problem as you denoted though.

             
    • Nobody/Anonymous

      Thanks Paul,

      When I have util.h, util.c, main.c and Makefile.win in the folder C:\Dev-Cpp\bin (the same fglder as make.exe) the command

      make -f Makefile.win

      works beautifully!

      However if I move the files util.h, util.c, main.c and Makefile.win in the folder C:\Dev-Cpp\bin\htw and try

      make -f htw\Makefile.win

      then I get the error message

      main: *** No rule to make target 'main.c' needed by 'main.o'. Stop

      Surely there must be a way to execute a Makefile even if it's not in the same folder as make.exe???

       
    • Nobody/Anonymous

      Instead of

      make -f htw\Makefile.win

      try

      make -f htw/Makefile.win

      (Note the forward slash as the directory separator, instead of backslash in "htw\Makefile.win ").
      Make is a unix thing, it doesn't understand "\".

       
      • Nobody/Anonymous

        No / insted of \ is not the problem

        in the folder htw I have util.h, util.c, main.c and Makefile.win

        The file Makefile I tried now was

        htw\util.o: htw\util.c htw\util.h
        gcc -c -g htw\util.c -o htw\util.o
        htw\main.o: htw\main.c htw\util.h
        gcc -c -g htw\main.c -o htw\main.o

        The strange thing is that util.o is generated in htw just as I want to, but main.o is not generated??

        If I instead write

        htw\main.o: htw\main.c htw\util.h
        gcc -c -g htw\main.c -o htw\main.o
        htw\util.o: htw\util.c htw\util.h
        gcc -c -g htw\util.c -o htw\util.o

        then main.o is generated but not util.o

        So it seems that the first command is executed but not the second. Any help appreciated.

         
      • Nobody/Anonymous

        You are right that in some cases you must use forward slash / (for instance when defining macros in makefile) while in other cases it suffices with backslah . But it's seems hard to tell when to use / and when to use .

         
    • Nobody/Anonymous

      Hey,
      util.o is not generated, because any target which is not a prerequisite of the first target (maybe there is a way to change this "first target" restriction, consult the make tutorial) is not, unless you call make explicitly with the target name. If you add the target for main.exe (or simply main) at the top of your makefile, you'll see that everything works.
      NB

       
    • Nobody/Anonymous

      Thank you very much all for the help. Now it works. Here is a summary

      1. Suppose you have yor source files util.c, util.h and main.c in a directory C:\mycode\htw. In the same directory create the file Makefile.win (the name doesn't matter) containing

      Makefile.win

      Observe that you must use slash instead of backslash in the macro on the next line

      BP = C:/Dev-Cpp/bin/
      main: main.o util.o
      $(BP)gcc -o main main.o util.o
      util.o: util.c util.h
      $(BP)gcc -c -g util.c -o util.o
      main.o: main.c util.h
      $(BP)gcc -c -g main.c -o main.o

      1. Open command prompt and navigate to htw directory by entering
        cd C:\mycode\htw

      3.Enter the following command
      C:/Dev-Cpp/bin/make -f Makefile.win

      1. Look in the htw directory. You should see three files created: util.o, main.o and main.exe.

      Frank

       
      • Nobody/Anonymous

        All is well, but weren't you trying to manage to make things work without navigating to the directory holding Makefile.win?

         
        • Nobody/Anonymous

          No, the only thing I wanted to do is to make it work when I had make.exe and gcc.exe in one directory while having source files and Makefile.win in another directory. I could as well navigated to the directory where make.exe was located but then I would have to put pathname in front of every source file and object file and that would be much more paths then putting the paths in front of make.exe and gcc.exe. In the first case I think that it ould be 15 pathnames, but in the second only 4.

           
          • Nobody/Anonymous

            Anyways, glad to see that you solved your problem :)

             
    • Nobody/Anonymous

      you may have to specify an 'all' target.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.