הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Hello Make

make intro

Assuming we have the following source and header files in our project:

  • source1.c source2.c source3.c
  • header1.h header2.h
runme: source1.o source2.o source3.o
        cc source1.o source2.o source3.o -o runme
 
source1.o: source1.c header1.h
        cc -c source1.c
 
source2.o: source2.c header2.h
        cc -c source2.c
 
source3.o: source3.c header1.h header2.h
        cc -c source3.c
 
clean:
        rm runme source1.o source2.o source3.o

Here we build a program called edit, composed of various components. Note the dependency tree in this one - Everything depends on defs.h, but only some components depend on other .h files

     edit : main.o kbd.o command.o display.o \
            insert.o search.o files.o utils.o
             cc -o edit main.o kbd.o command.o display.o \
                        insert.o search.o files.o utils.o
 
     main.o : main.c defs.h
             cc -c main.c
     kbd.o : kbd.c defs.h command.h
             cc -c kbd.c
     command.o : command.c defs.h command.h
             cc -c command.c
     display.o : display.c defs.h buffer.h
             cc -c display.c
     insert.o : insert.c defs.h buffer.h
             cc -c insert.c
     search.o : search.c defs.h buffer.h
             cc -c search.c
     files.o : files.c defs.h buffer.h command.h
             cc -c files.c
     utils.o : utils.c defs.h
             cc -c utils.c
     clean :
             rm edit main.o kbd.o command.o display.o \
                insert.o search.o files.o utils.o
# Using the @ sign, we prevent make
# From echoing our commands. 
# And, the # sign means a comment
 
runme: source1.o source2.o source3.o
        @cc source1.o source2.o source3.o -o runme
 
source1.o: source1.c header1.h
        @cc -c source1.c
 
source2.o: source2.c header2.h
        @cc -c source2.c
 
source3.o: source3.c header1.h header2.h
        @cc -c source3.c
 
clean:
        @rm -f runme source1.o source2.o source3.o
 
runme: source1.o source2.o source3.o
        @echo "Building Executable runme"
        @cc source1.o source2.o source3.o -o runme
 
source1.o: source1.c header1.h
        @cc -c source1.c
 
source2.o: source2.c header2.h
        @cc -c source2.c
 
source3.o: source3.c header1.h header2.h
        @cc -c source3.c
 
clean:
        @echo "Cleaning Up"
        @rm -f runme source1.o source2.o source3.o
# Using variables, we can store a word or a list of words
# and use it multiple times in our file, without
# repeating the same list over and over
 
objects = source1.o source2.o source3.o
output = runme
RM = rm -f
 
$(output): $(objects)
        @echo "Building Executable $(output)"
        @cc $(objects) -o $(output)
 
source1.o: source1.c header1.h
        @cc -c source1.c
 
source2.o: source2.c header2.h
        @cc -c source2.c
 
source3.o: source3.c header1.h header2.h
        @cc -c source3.c
 
clean:
        @echo "Cleaning Up"
        @$(RM) $(output) $(objects)
 
     objects = main.o kbd.o command.o display.o \
               insert.o search.o files.o utils.o
 
     edit : $(objects)
             cc -o edit $(objects)
     main.o : main.c defs.h
             cc -c main.c
     kbd.o : kbd.c defs.h command.h
             cc -c kbd.c
     command.o : command.c defs.h command.h
             cc -c command.c
     display.o : display.c defs.h buffer.h
             cc -c display.c
     insert.o : insert.c defs.h buffer.h
             cc -c insert.c
     search.o : search.c defs.h buffer.h
             cc -c search.c
     files.o : files.c defs.h buffer.h command.h
             cc -c files.c
     utils.o : utils.c defs.h
             cc -c utils.c
     clean :
             rm edit $(objects)
 
course: