CMAKE---Multiple directories
Overview
CMake is a directory-based project management tool. If your project has multiple directories, more CMakeLists.txt
files should be created in your project directories.
Situation
As the following directories, each directory is a standalone module. For example:
- the root target has its own source file(s) in
${ROOT}/src
directory. hello_object_lib
is aOBJECT
target, and it has its own including paths. So we use a CMakeLists.txt file for target management.hello_shared_lib
is aSHARED
library target, the root project will link this shared library or provide for other projects.hello_static_lib
is aSTATIC
library target, this target will generate a static library file for other projects.
1 | tree |
The content of root CMakeLists.txt
is as follows. Each sub-directory should be added in this CMakeLists.txt.
In this CMakeLists.txt, we define a executable target multiple_exec
, all of the other targets will be linked to this executable target. There’s a difference should be noticed. OBJECT
target cannot be linked into other targets using target_link_libraries
command before CMake V3.12. So if you use the older CMake version, you should use the
1 | cmake_minimum_required(VERSION 3.21) |
References
CMAKE---Multiple directories
https://wtffqbpl.github.io/2022/10/31/CMAKE-Multiple-directories/