Libraries, And Dependencies#
Libraries And Executables#
Raw toolchain building a library: Object Code Archives/Static Libraries
Nothing is free: build instructions become more complex
Executable do not stand alone anymore
⟶ depend on a library
⟶ Dependency graph (directed and acyclic, ideally)
CMake terminology: graph nodes are called “targets” (inherited from
maketerminology)
cmake_minimum_required(VERSION 3.16)
project(Demo)
add_library(hello # <-- target "hello"
hello.h hello.c
hello-name.h hello-name.c)
add_executable(hello-first hello-first.c) # <-- target "hello-first"
target_link_libraries(hello-first hello) # <-- "hello-first" depends on "hello"
add_executable(hello-second hello-second.c) # <-- target "hello-second"
target_link_libraries(hello-second hello) # <-- "hello-second" depends on "hello"
Note
Header files are not compiled, but adding them is convenient for IDE usage.
Targets And Dependencies#
Executables depend on libraries
⟶ dependencies
hello-firstdepends onhellohello-seconddepends onhello
target_link_libraries(hello-first hello) # <--- "hello-first" depends on "hello"
target_link_libraries(hello-second hello) # <--- "hello-second" depends on "hello"
Executables may depend on libraries
Libraries may depend on libraries
(Nothing ever depends on an executable)
⟶ Directed Acyclic Graph (DAG)
Visualizing Dependencies#
Project sanity: knowing your dependencies
⟶ Graphviz package
Generating
dot files#$ pwd
/path/to/build/directory
$ cmake --graphviz=Demo.dot ~/work/jfasch-home/trainings/material/soup/cmake/05-static-libraries/
$ ls -l *.dot*
-rw-r--r--. 1 jfasch jfasch 1260 Jul 13 19:31 Demo.dot
-rw-r--r--. 1 jfasch jfasch 336 Jul 13 19:31 Demo.dot.hello.dependers
-rw-r--r--. 1 jfasch jfasch 222 Jul 13 19:31 Demo.dot.hello-first
-rw-r--r--. 1 jfasch jfasch 225 Jul 13 19:31 Demo.dot.hello-second
Convert into viewable image#
$ dot -Tpng Demo.dot > Demo.png