Simplifying build commands: cmake + make = cbuild

If you are working with c/c++ you might find yourself compiling, cleaning and testing your code with make. Hopefully, you value a bit your time and you use cmake (although cmake has a ton of caveats that I can’t list here).

In any case you probably find yourself executing quite a bit of times the following commands:

cd build
cmake ..
make -j 8
cd ..

Sometimes you also want to clean your code before:

rm -rf build
mkdir build
cd build
cmake ..
make -j 8
cd ..

Or, in a softer way:

cd build
make clean
make -j 8
cd ..

Ever testing? You are a good developer right?

cd build
make -j 8
make test
cd ..

Well, I became tired of typing all these commands and wrote a python script to be run at the root of your project that just does the above depending on the options you give it, so the commands you end up typing are at most:

  • Regular build: cbuild
  • Clean & build: cbuild --clean or cbuild -c
  • Build & test: cbuild --test or cbuild -t
  • Clean & build & test: cbuild -c -t

And, using some aliases:

  • Regular build: cb
  • Clean & build: cbc
  • Build & test: cbt
  • Clean & build & test: left to the reader ;)

The code for the cbuild command looks like this:

To add this to your command line, in the least intrusive way (the other way is to add it to your PATH), do:

  1. Save the script somewhere, for example: ${HOME}/path/to/cbuild.
  2. Write 4 aliases in your ~/.bashrc:
alias cbuild="${HOME}/path/to/cbuild"
alias cb="cbuild"
alias cbc="cbuild --clean"
alias cbt="cbuild --test"