How to add a Makefile post-build step
24 September, 2010 § 1 Comment
I spend most of my time working in Visual Studio and like its ability for performing post-build steps. These can be used to copy files, register DLLs, or run unit tests.
In my work at MSU I need to use a makefile and have been writing my code test-first. As such, I found I was spending a lot of time typing
make and then typing
./runTests. Ideally, I could just have the tests run automatically after each build.
Looking around on the internet for quick steps to add a post-build step to make wasn’t so fruitful. I ran in to a bunch of old forum posts that mentioned Visual Studio-generated makefiles with post-build steps. A little thinking about the idea sprang the solution straight to mind.
It turns out it is really simple and quite intuitive. Here’s my current makefile:
CC=g++ CFLAGS=-c -Wall -pedantic-errors -Wextra -Wunused -Werror all: findRoute findRoute-test runTests rebuild: clean all runTests: findRoute-test ./findRoute-test.exe findRoute: main.o cities.o flight_charges.o traffic_controller.o $(CC) main.o cities.o flight_charges.o traffic_controller.o -o findRoute findRoute-test: main-test.o cities.o flight_charges.o traffic_controller.o $(CC) main-test.o cities.o flight_charges.o traffic_controller.o -o findRoute-test main.o: main.cpp $(CC) $(CFLAGS) main.cpp cities.o: cities.cpp $(CC) $(CFLAGS) cities.cpp flight_charges.o: flight_charges.cpp $(CC) $(CFLAGS) flight_charges.cpp traffic_controller.o: traffic_controller.cpp $(CC) $(CFLAGS) traffic_controller.cpp main-test.o: main-test.cpp $(CC) $(CFLAGS) main-test.cpp clean: rm -rf *o findRoute*
runTests step above states that the
findRoute-test step is a prerequisite. If that step passes, then it simply executes the next line, that being the test executable.
I found this very intuitive as it is no different than any other executable like the compiler. I hope this helps someone else who is thinking of doing the same or a variation.
Do you see anything above that could be improved? I love getting comments that help me get better.