24 June 2022

A small guide to creating your own library using CMake. Please note that this post is prepared for myself to remind me how to use it after a long time of forgetfulness.

  1. project() and cmake_minimum_required() is mandatory.
  2. ${PROJECT_NAME} is a variable holds our project name which is decleared in project() command.
  3. If you have your library files in directories, it means that you have multi-level structure. To achieve an accurate Makefile, you need to specify these directories with using add_subdirectory(). It should not be forgetten that each directory needs their own CMakeLists.txt files that contains add_library().
  4. target_include_directory(): It lets you to include libraries without its full path if they are marked with add_library(). Consider this conversion: "Adder/adder.h" to <adder.h>.
  5. target_link_directory(): It creates a library inside the build folder, and a library file to use.

Published in cppcheatsheetslang::en