Project Layout
Explore how to layout a professional CMake project by defining logical targets, using object libraries, and organizing source and test directories. Understand the roles of static and shared libraries and how to manage dependencies and project files effectively.
We'll cover the following...
To build any project, we should start with a clear understanding of what logical targets are going to be created in it. In this case, we'll follow the structure shown in the following figure:
Let's explore the structure by following the build order. First, we'll compile calc_obj, which is an object library. We did mention object libraries a few times in the course, but we didn't actually introduce them as a concept. Let's do this now.
Object libraries
Object libraries are used to group multiple source files under a single logical target and are compiled into the (.o) object files during a build. To create an object library, we use the same method as with other libraries with the OBJECT keyword:
add_library(<target> OBJECT <sources>)
Object files produced during the build can be added as compiled elements to other targets with the $<TARGET_OBJECTS:objlib> generator expression:
add_library(... $<TARGET_OBJECTS:objlib> ...)add_executable(... $<TARGET_OBJECTS:objlib> ...)
Alternatively, add them as ...