.. include:: Structure - Subdirectories, Dependencies, and Include Paths =========================================================== .. topic:: Source .. code-block:: console 08-structure/ ├── CMakeLists.txt ├── bin │ ├── CMakeLists.txt │ ├── hello-first.c │ └── hello-second.c └── libhello ├── CMakeLists.txt ├── hello.h ├── hello.c ├── hello-name.h └── hello-name.c * :download:`08-structure/CMakeLists.txt` * :download:`08-structure/bin/CMakeLists.txt` * :download:`08-structure/bin/hello-first.c` * :download:`08-structure/bin/hello-second.c` * :download:`08-structure/libhello/CMakeLists.txt` * :download:`08-structure/libhello/hello.h` * :download:`08-structure/libhello/hello.c` * :download:`08-structure/libhello/hello-name.h` * :download:`08-structure/libhello/hello-name.c` Reorganization -------------- .. topic:: Documentation * `add_subdirectory() `__ * Descend into ``libhello/`` and ``bin/`` * Move files around * Move CMake code around * *Miss to care about include paths* .. code-block:: console :caption: Toplevel ``CMakeLists.txt`` add_subdirectory(libhello) add_subdirectory(bin) .. code-block:: console $ make [ 14%] Building C object libhello/CMakeFiles/hello.dir/hello.c.o [ 28%] Building C object libhello/CMakeFiles/hello.dir/hello-name.c.o [ 42%] Linking C shared library libhello.so [ 42%] Built target hello [ 57%] Building C object bin/CMakeFiles/hello-first.dir/hello-first.c.o /home/jfasch/work/jfasch-home/trainings/material/soup/cmake/code/bin/hello-first.c:1:10: fatal error: hello.h: No such file or directory 1 | #include "hello.h" | ^~~~~~~~~ compilation terminated. make[2]: *** [bin/CMakeFiles/hello-first.dir/build.make:76: bin/CMakeFiles/hello-first.dir/hello-first.c.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:144: bin/CMakeFiles/hello-first.dir/all] Error 2 make: *** [Makefile:136: all] Error 2 Dependencies, And *Target Properties* ------------------------------------- .. topic:: See also :doc:`/trainings/material/soup/misc/cmake/advanced/targets-properties/topic` will cover dependencies and *property propagation* in more detail. .. topic:: Documentation * `target_include_directories() `__ * Dependencies are in place .. image:: 08-structure/Demo.png :scale: 60% * Only code has been moved around * |longrightarrow| ``hello`` header files are now too far away from ``bin/`` targets * Let ``hello`` carry that information .. code-block:: console target_include_directories(hello PUBLIC .) * |longrightarrow| *propagated* to dependers * ``PUBLIC`` vs. ``PRIVATE`` * ``PUBLIC`` *propagated* to *dependers* (see/show ``.dependers`` dot file) so they can find the headers of targets they depend on * ``PRIVATE`` does not propagate. Purpose: e.g., one might structure (internally only) a target's source code into ``src/`` and ``inc/`` for example.