Basic Usage: Generators, Building#
Single Monolithic Executable#
Simple toolchain demo follows (see Toolchain: Basics)
Simplest; default output name:
a.out$ gcc hello-single.c $ ./a.out Hello World
Explicit output name
hello-single$ gcc -o hello-single hello-single.c $ ./hello-single Hello World
Wish list item: out-of-source build. Why?
Output files like
hello-worldneed to be added to.gitignoreSource might be on a medium that is more expensive; RAID for example.
Build for multiple architectures ⟶ cannot mix all in one single directory
…
All in all: source tree must be read-only (if only for hygiene)
⟶ CMake
Building With CMake#
cmake_minimum_required(VERSION 3.16)
project(Demo)
add_executable(hello-single hello-single.c)
CMakeLists.txtin each subdirectory that participates in the buildWe have only one so far, and that is trivial
cmake_minimum_required(): required by CMake; used to declare which features we will need (or fail)project(): used in many corners, e.g. package namesVERSIONparameter (optional); will use it later
add_executable()
Building With CMake: Separate Source And Build Directories#
Source directory, e.g.
~/My-Projects/01-singleVersion controlled
Build directory, e.g.
~/My-Builds/01-single-x86_64Not version controlled ⟶ does not interfere with version control (no need to
.gitignorebuild directory)Completely resurrectable
Not subject to backups
Cheap/fast storage medium (RAM filesystem?)
Simplest way of building
Change to build directory
Call
cmaketo buildMakefileskeleton (orninja) from the source directoryCall
make(orninja, for that matter) in the build directory
Makefile structure#$ cd ~/My-Builds/01-single-x86_64
$ cmake ~/My-Projects/01-single
-- The C compiler identification is GNU 12.2.1
-- The CXX compiler identification is GNU 12.2.1
-- 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 (0.7s)
-- Generating done (0.0s)
-- Build files have been written to: /home/jfasch/My-Builds/01-single-x86_64
make#$ make
[ 50%] Building C object CMakeFiles/hello-single.dir/hello-single.c.o
[100%] Linking C executable hello-single
[100%] Built target hello-single
Executables Consisting Of Multiple Sources#
Programs are rarely so simple
Separate compilation units
⟶ potential code re-use
add_executable()can take more than one source file parameters
cmake_minimum_required(VERSION 3.16)
project(Demo)
add_executable(hello-modular hello-main.c hello.c) # <-- two compilation units
Problem: Duplicate Compilation#
Duplication of build rules (
hello.cis built twice)Show
CMakeFiles/
cmake_minimum_required(VERSION 3.16)
project(Demo)
add_executable(hello-first hello-first.c hello.c)
add_executable(hello-second hello-second.c hello.c)
Problem: More Functionality#
Quick look into
hello-name.[hc], andhello-second.cBoth
hello.oandhello-name.oare compiled twice ⟶ quadratic build time explosion
cmake_minimum_required(VERSION 3.16)
project(Demo)
add_executable(hello-first hello-first.c hello.c hello-name.c)
add_executable(hello-second hello-second.c hello.c hello-name.c)
Solution: Libraries#
Library: bag of object files
Built only one
Come in static and shared flavors