Cmake

From Andreida

So, what is cmake? CMake allows you to tell what should happen and cmake creates a whole lot of logic around that and makes it happen.

If you want to learn how to do it: https://cmake.org/getting-started/

If you do the tutorial, be prepared, it is not very good. It is often not clear which file in which folder you should modify, which WILL create a mess.

While creating your CMakeLists.txt you will have a lot of tries and errors normally. Ease your pain by putting a Makefile in the same folder and using this to organize the use of CMake. Remember to use TAB instead of SPACES where it is needed. Sorry, I have to use spaces here.

LIB_DIR=lib
EXE_DIR=bin
DIR_BUILD=build

.PHONY: all
all:
     cmake -B $(DIR_BUILD)
     cmake --build $(DIR_BUILD)

.PHONY: clean
clean:
     rm -rf $(LIB_DIR) $(EXE_DIR) $(DIR_BUILD)

 # clang-format
 .PHONY: cf
 cf:
 #   @clang-format -style=file -i include/*.h
 #   @clang-format -style=file -i *.h
 #   @clang-format -style=file -i *.cpp
     @clang-format -style=file -i *.cxx

.PHONY: list
list:
     @$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# F    inished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e     '^$@$$'

This will give you the options to

  • build
  • clean
  • format the code with clang-format, but perhaps you'd prefer this to be done via CMake
  • list all the options for the Makefile

Start with

make list