What can I do to fix this problem? I’m new to emacs, Unix, and makefiles. Please explain what I’m doing wrong and how I can fix it. I’m sure it’s a simple problem
progA: yesno.h yesno.cpp
g++ -DDEBUG -c yesno.cpp yesno.h
progB: guess.cpp
g++ -DDEBUG -c guess.cpp
progC: yesno.o guess.o
g++ -DDEBUG -o guess guess.o yesno.o
My error is:
Your makefile does too much work when only guess.cpp has been changed:
g++ -c yesno.cpp yesno.h
g++ -c guess.cpp
g++ -o guess guess.o yesno.o
Your makefile would recompile everything each time.
You don’t need the first four lines;
makeknows how to create object files from source. Or, alternatively, you need to revise those 4 lines into 2, and put them at the end, and change the target names:The first target is the one that is built by default, so that ensures that
guess(the program) is built. The command line omits the-DDEBUGsince that only affects source code compilation and there is no source in that link line (though, if using macros as one would in an advancedmakefile, I’d be fine with the options such as-DDEBUGappearing in the link line).The third line says that
guess.odepends onyesno.h; this is a guess (on my part). Themakeprogram already knows how to convertguess.cpptoguess.o; it just needs the extra information that it also uses theyesno.hheader. (If it doesn’t, how does it know about the functions defined inyesno.cpp?)The last line says that
yesno.odepends onyesno.h; again,makeknows how to compileyesno.cpptoyesno.obut needs the extra information about the header.This should only recompile
guess.owhen onlyguess.cppchanges; it should only recompileyesno.owhenyesno.cppchanges; it should recompile both object files whenyesno.hchanges. If any of the source files changes, the program will be relinked; if no source files change, then themakecommand should do nothing (or simply report that there is nothing to do).