2.1.1. Basic build with CMakeΒΆ

If we want to achieve same results (and even more) four the source code as listed in Final source code for basic compilation, we would not write the Makefile. But rather, we will write a CMakeLists.txt.

A very basic CMakeLists.txt for that purpose would look like this.

 6    greeting_en
 7    main.c greeting_en.c
10    greeting_fr
11    main.c greeting_fr.c
14    greeting_es
15    main.c greeting_es.c

To compile that file with GCC, the command would be.

mkdir _build
cd _build
cmake .. -G "Unix Makefiles"

When the above command completes successfully, the compilation output would be:

-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake/_build

The generated Makefile would look as below. (Note, the contents may change from platform to platform )

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.22

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.

# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.

# Disable VCS-based implicit rules.
% : %,v

# Disable VCS-based implicit rules.
% : RCS/%

# Disable VCS-based implicit rules.
% : RCS/%,v

# Disable VCS-based implicit rules.
% : SCCS/s.%

# Disable VCS-based implicit rules.
% : s.%

.SUFFIXES: .hpux_make_needs_suffix_list

# Command-line flag to silence nested $(MAKE).

#Suppress display of executed commands.

# A target that is always out of date.
.PHONY : cmake_force

# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E rm -f

# Escaping for special characters.

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake/_build

# Targets provided globally by CMake.

# Special rule for the target edit_cache
	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
	/usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
	@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
	/usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
	$(CMAKE_COMMAND) -E cmake_progress_start /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake/_build/CMakeFiles /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake/_build//CMakeFiles/progress.marks
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
	$(CMAKE_COMMAND) -E cmake_progress_start /home/c6h6/data/p/book-tests/pgh_tech-sw-build-system/book/code/0200_cmake/_build/CMakeFiles 0
.PHONY : all

# The main clean target
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

# Target rules for targets named greeting_en

# Build rule for target.
greeting_en: cmake_check_build_system
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 greeting_en
.PHONY : greeting_en

# fast build rule for target.
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/build
.PHONY : greeting_en/fast

# Target rules for targets named greeting_fr

# Build rule for target.
greeting_fr: cmake_check_build_system
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 greeting_fr
.PHONY : greeting_fr

# fast build rule for target.
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/build
.PHONY : greeting_fr/fast

# Target rules for targets named greeting_es

# Build rule for target.
greeting_es: cmake_check_build_system
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 greeting_es
.PHONY : greeting_es

# fast build rule for target.
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/build
.PHONY : greeting_es/fast

greeting_en.o: greeting_en.c.o
.PHONY : greeting_en.o

# target to build an object file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/greeting_en.c.o
.PHONY : greeting_en.c.o

greeting_en.i: greeting_en.c.i
.PHONY : greeting_en.i

# target to preprocess a source file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/greeting_en.c.i
.PHONY : greeting_en.c.i

greeting_en.s: greeting_en.c.s
.PHONY : greeting_en.s

# target to generate assembly for a file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/greeting_en.c.s
.PHONY : greeting_en.c.s

greeting_es.o: greeting_es.c.o
.PHONY : greeting_es.o

# target to build an object file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/greeting_es.c.o
.PHONY : greeting_es.c.o

greeting_es.i: greeting_es.c.i
.PHONY : greeting_es.i

# target to preprocess a source file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/greeting_es.c.i
.PHONY : greeting_es.c.i

greeting_es.s: greeting_es.c.s
.PHONY : greeting_es.s

# target to generate assembly for a file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/greeting_es.c.s
.PHONY : greeting_es.c.s

greeting_fr.o: greeting_fr.c.o
.PHONY : greeting_fr.o

# target to build an object file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/greeting_fr.c.o
.PHONY : greeting_fr.c.o

greeting_fr.i: greeting_fr.c.i
.PHONY : greeting_fr.i

# target to preprocess a source file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/greeting_fr.c.i
.PHONY : greeting_fr.c.i

greeting_fr.s: greeting_fr.c.s
.PHONY : greeting_fr.s

# target to generate assembly for a file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/greeting_fr.c.s
.PHONY : greeting_fr.c.s

main.o: main.c.o
.PHONY : main.o

# target to build an object file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/main.c.o
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/main.c.o
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/main.c.o
.PHONY : main.c.o

main.i: main.c.i
.PHONY : main.i

# target to preprocess a source file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/main.c.i
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/main.c.i
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/main.c.i
.PHONY : main.c.i

main.s: main.c.s
.PHONY : main.s

# target to generate assembly for a file
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_en.dir/build.make CMakeFiles/greeting_en.dir/main.c.s
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_fr.dir/build.make CMakeFiles/greeting_fr.dir/main.c.s
	$(MAKE) $(MAKESILENT) -f CMakeFiles/greeting_es.dir/build.make CMakeFiles/greeting_es.dir/main.c.s
.PHONY : main.c.s

# Help Target
	@echo "The following are some of the valid targets for this Makefile:"
	@echo "... all (the default if no target is provided)"
	@echo "... clean"
	@echo "... depend"
	@echo "... edit_cache"
	@echo "... rebuild_cache"
	@echo "... greeting_en"
	@echo "... greeting_es"
	@echo "... greeting_fr"
	@echo "... greeting_en.o"
	@echo "... greeting_en.i"
	@echo "... greeting_en.s"
	@echo "... greeting_es.o"
	@echo "... greeting_es.i"
	@echo "... greeting_es.s"
	@echo "... greeting_fr.o"
	@echo "... greeting_fr.i"
	@echo "... greeting_fr.s"
	@echo "... main.o"
	@echo "... main.i"
	@echo "... main.s"
.PHONY : help

# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

For more information on CMake, see here