0
votes

So I have this make file

#makefile to build a program

#program depends on components: name and main
myname:     main.o name.o
       g++ -c -g name.cpp

# name.cpp has it's own header file
name.o:     name.cpp name.h
       g++ -c -g  name.cpp

# main.cpp also user the header file name.h
main.o:     main.cpp name.h
       g++ -c -g main.cpp

clean:
       /bin/rm -f myname *.o

I need to modify the makefile to include a rule that creates a backup of the source files, makefile, and readme in an archive directory. This rule needs to create a tar.gz file containing the files I mentioned. The.tar.gz file should be placed in a directory named backup within my current working directory. This backup rule should be executed by 'make backup' and the output should be a .tar.gz file in the backup directory. This backup rule should create the directory if it does not already exist.

Any help would be greatly appreciated

EDIT: the makefile already works as intended as is, I just need to figure out how to add this rule.

1

1 Answers

0
votes
PROG := myname
HDR := name.h
SRC := main.cpp name.cpp
OBJ := main.o name.o
ARCHIVE := tar.gz
TARFILES := $(SRC) $(HDR) makefile readme

# original
myname: main.o name.o
        g++ -c -g name.cpp

# suggested replacement
# $(PROG): $(OBJ)
#       g++ -g $(OBJ) -o $@

name.o: name.cpp name.h
        g++ -c -g  name.cpp

main.o: main.cpp name.h
        g++ -c -g main.cpp

clean:
        /bin/rm -f $(PROG) $(OBJ)

.PHONY: backup
backup:
        @mkdir -p $@
        tar zcvf $@/$(ARCHIVE) $(TARFILES)