

GNU make and deleting intermediate files
source link: https://peter.bourgon.org/blog/2009/10/10/gnu-make-and-deleting-intermediate-files.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

GNU make and deleting intermediate files
2009 10 10
Over the months and years I’ve developed a pretty useful Makefile template that I like to use with any C++ project that exceeds a couple files. It’s not super- powerful but it has the advantage of being easily understandable, and I value maintainability and readability pretty highly. I structure my project like:
include/
Constants.h
MyClass.h
MyOtherClass.h
src/
MyClass.cpp
MyOtherClass.cpp
my_executable_name.cpp
obj/
*initially empty*
bin/
my_executable_name
third-party/
some_library/
lib/
include/
test/
test_1.cpp
test_2.cpp
Then, the Makefile can work pretty well like this:
CC = g++
OPT = -Wall -ggdb
OBJ = \
obj/MyClass.o \
obj/MyOtherClass.o
INC = -I./include -I./third-party/boost/include/boost-1_37 -I./third-party/other_library/include
LIB = -L./third-party/boost/lib -lboost_thread-xgcc40-mt -L./third-party/other_library/lib -lsomethingelse
MY_EXECUTABLE = bin/my_executable_name
TEST = \
test/test_1 \
test/test_2
default: $(MY_EXECUTABLE)
obj/%.o: src/%.cpp include/%.h
$(CC) -c $(OPT) $(INC) -o $@ $<
bin/%: src/%.cpp $(OBJ)
$(CC) $(OPT) $(INC) $(LIB) -o $@ $^
test/%: test/%.cpp
$(CC) $(OPT) $(INC) $(LIB) -o $@ $^
all: $(MY_EXECUTABLE) $(TEST) Makefile
clean:
rm -f $(OBJ) $(MY_EXECUTABLE) $(TEST)
test: $(TEST)
But make has this little thing where it likes to delete what it considers ‘intermediate’ files after it’s done doing whatever it’s doing. In my case, I have make automatically generate the object files, so whenever I run a build it destroys them afterwards. Reading up on it, it seems like somebody in maketown decided that was a good tradeoff: that they had to sacrifice build efficiency for correctness. I’m not sure how correctness would be affected if they kept those object files around, but maybe that’s why I’m not on the make team. In the meanwhile, I’ve found a workaround: you need to mark your object files as “precious”, like so:
.PRECIOUS: obj/%.o
obj/%.o: src/%.cpp include/%.h
$(CC) -c $(OPT) $(INC) -o $@ $<
This prevents them from being deleted, but also apparently may cause problems if
the build is interrupted. I trust you are smart enough to make clean ; make
in
that situation.
Recommend
-
9
John Fremlin's blog: Freeing space from open files on UN*X when deleting doesn't workWaiting for updates: connectedPosted 2017-12-29 23:00:00 GMTYour disk is filling up,...
-
4
Intermediate Rails: Understanding Models, Views and Controllers I’m glad people liked the introduction to Rails; now you scallawags get to...
-
8
Create ZIP files on HTTP request without intermediate files using ASP.NET MVC, Razor Pages, and endpoints Niels Swimberghe - 3/21/2021 - .NET Follow me on
-
2
Project Introduction In this tutorial, we’ll be creating a countdown timer that asks the user how much time (in seconds) they want to set the timer for — once time is up, print out “Blast Off!”.
-
6
AWS S3 Glacier Deep Archive - Difficulty deleting files with accents June 15, 2021 A few days ago, my perso...
-
15
Facebook, Telegram and Instagram charged by Russian court Russia filed fresh accusations against Facebook, Google, Telegram, and Twitter, claiming that they failed to delete unlawful material. The K...
-
9
Converting a whole directory tree of FLAC files to opus/vorbis and deleting the originals
-
6
deleting, removing and renaming foldersS&P Futures3,989.25+20....
-
6
PowerToys will soon show what processes prevent you from deleting files...
-
7
Deleting Past Versions of All Files in a SharePoint Folder » Stuart’s MATLAB Videos In my last post, I developed a script to delete the past...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK