Thread: Makefile question

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Makefile question

    In my assignment, the professor asks to remove all
    generated files with a "clean" target. The problem with that
    is that the user can specify the names of the files he wants
    to generate. The user can generate multiple files. So I've set up
    the following:

    Code:
    my_files = main.c funcs.c funcs.h Makefile README
    dir = `ls`
    
    clean:
    @for file in $(dir); do\
         @for my_file in $(my_files); do\
              if [ $$file != $$my_file ]; then\
                   rm $$file;\
              fi;\
          done;\
    done;
    That gives me a syntax error. Any suggestions.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I'm not asking how to write a Makefile. I just want to delete the files in the directory that aren't needed to compile my program. Of course, the files can be any name. I pretty sure the professor just want us to delete the object files, but I'm just curious whether it is possible to remove other generated non-object files (such as ASCII files).
    Thanks,
    Yasir

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Apparently you missed this part.
    Code:
    clean:
    	rm -rf *o hello
    In this example we see a target called clean. It is useful to have such target if you want to have a fast way to get rid of all the object files and executables.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I'm sorry if I'm not being clear. Let me clarify. These are the object files that are generated: funcs.o and main

    I can get rid of those easily. My program has many command line options that allow the user to generate many different types of text files (we're writing a Pascal compiler with many debugging files). So suppose the user generates the following files:
    asdf asdfiweo asdf adflas asdf alskdf

    and so on. I want my Makefile to get rid of those as well with a clean target, but I still want to keep funcs.c, funcs.h, main.c, Makefile, and README.
    Thanks,
    Yasir

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    By the way, my program has many more source files, but I want to simplify the problem.

  7. #7
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    is $$my_file a member of my_files? how does that work? I'm not too familiar with makefile syntax.
    PHP and XML
    Let's talk about SAX

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Yasir_Malik
    So suppose the user generates the following files:
    asdf asdfiweo asdf adflas asdf alskdf
    If these files follow a naming convention, you're apparently familiar with how that is done.

    If these files have names known ahead of time, just add these names as dependencies to your clean rule and use the 'all dependents' predefined macro ($**, with Borland's make).

    If you have filenames that don't follow a naming convention and are unknown ahead of time, I don't think you can automate the process with the make utility.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    maybe instead of trying to solve the problem that he's already found a solution for, you guys could help him find the syntax error he's complaining about...that's all he wants.
    PHP and XML
    Let's talk about SAX

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    The generated files do not follow any pattern. The files can be anything, like oiaud asldfnlmasdf wer,v asrwaer ,awerpaeas rtytyr ....

    I know that I do not have to delete all of the generated text files (I just have to delete the object files), but it would be cool if I could.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    is $$my_file a member of my_files? how does that work? I'm not too familiar with makefile syntax.
    Makefiles are a mystery to me, too. 'files' is kind of like a string, and you can loop through each word in the string. Each word would be stored in 'file'. It's kind of like your "foreach" statement.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    To me the following looks like shell script, not a makefile.
    Code:
    dir = `ls`
    
    clean:
    @for file in $(dir); do\
         @for my_file in $(my_files); do\
              if [ $$file != $$my_file ]; then\
                   rm $$file;\
              fi;\
          done;\
    done;
    But it may just be a make utility that I am not familiar with. Which make utility are you using?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    I'm using the make utility that with my distribution of NetBSD. I've used for loops before in Makefiles. Also doing dir = `ls` does store the each file into dir. I'm guessing make inherited many of the features from sh, but the inheritance was degenerate.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If it's like GNU make, maybe something here can help. It doesn't look like I can.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, I asked around, and here's a response I got from an expert C programmer:
    Code:
    my_files = CVS README Makefile funcs.h funcs.c main.c
    
    clean:
    	for file in `ls`; do \
    		for my_file in $(my_files) ; do \
    			if [ "$$file" = "$$my_file" ]; then file=""; fi; \
    		done; \
    		if [ "$$file" != "" ] ; then rm $$file ; fi; \
    	done;
    As you can see, it's pretty close to my solution. However, what does file="" do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Compilation Question
    By chacham15 in forum C Programming
    Replies: 10
    Last Post: 10-12-2008, 08:15 PM
  2. A question about an interesting Makefile
    By meili100 in forum Tech Board
    Replies: 2
    Last Post: 08-12-2008, 03:56 PM
  3. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Makefile Newbie: Inheritance Question
    By Ashes999 in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2003, 02:34 AM