C++ - Makefile Good Practice -
i have makefile works how i'm using it, tell me if i'm doing practice? or if there better, cleaner or more efficient way achieve goal reaching?
here makefile code.
# object files either reference or create objects = proj2.o blackjack.o deck.o card.o hand.o player.o # executable file created exec = proj2.out # c++ flags use compilation cxxflags = -wall # c++ compiler use compilation cxx = g++ # section called on 'make' # call compile, , call clean all: compile clean # perform action on object files (may or may not exist) # makefile implicitly compile .o files needed # compile them exec file listed compile: $(objects) $(cxx) $(cxxflags) -o $(exec) $(objects) # section called after compilation completed # clean existing .o files listed in directory clean: rm -f *.o
here terminal output when call make
.
g++ -wall -c -o proj2.o proj2.cpp g++ -wall -c -o blackjack.o blackjack.cpp g++ -wall -c -o deck.o deck.cpp g++ -wall -c -o card.o card.cpp g++ -wall -c -o hand.o hand.cpp g++ -wall -c -o player.o player.cpp g++ -wall -o proj2.out proj2.o blackjack.o deck.o card.o hand.o player.o rm -f *.o
is practice use makefile this? specifically, doing cleaning part of makefile correctly?
you should not make all
depend on clean
@ all. doing ensuring every time run make
, have recompile everything. if want using make
useless: write shell script compiles , links code.
the clean
target should separate target , if want clean workspace run make clean
explicitly.
the other problem makefile link rule lists compile
target, builds $(exe)
. it's never idea have rule create file not target told make build. ensure this, use $@
target generate. rewrite this:
compile: $(exe) $(exe): $(objects) $(cxx) $(cxxflags) -o $@ $^
Comments
Post a Comment