Libraries, And Dependencies¶
Libraries And Executables¶
Raw toolchain in action: Object Code Archives/Static Libraries
Defining a library
ADD_LIBRARY(hello # <--- target "hello" hello.h hello.c hello-name.h hello-name.c)
Note
Header files are not compiled, but adding them is convenient for IDE usage.
Executables only contain main code (all code that depends on is in the library)
ADD_EXECUTABLE(hello-first hello-first.c) # <--- target "hello-first" ADD_EXECUTABLE(hello-second hello-second.c hello.c hello-name.c)
$ cmake ~/work/jfasch-home/trainings/material/soup/cmake/05-static-libraries/
...
$ make
[ 57%] Linking C executable hello-first
/usr/bin/ld: CMakeFiles/hello-first.dir/hello-first.c.o: in function `main':
hello-first.c:(.text+0x10): undefined reference to `hello'
/usr/bin/ld: hello-first.c:(.text+0x29): undefined reference to `hello_name'
collect2: error: ld returned 1 exit status
⟶ Dependencies!
Targets And Dependencies¶
Executables depend on libraries
⟶ dependencies
hello-first
depends onhello
hello-second
depends 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
$ pwd
/tmp/build
$ 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
$ dot -Tpng Demo.dot > Demo.png