I'm trying to convert my previous qmake approach to cmake. I have a static lib called shared that links with different applications as a common ground. Each application has it's own static lib called common to link with the different targets of the same application (standalone, plugin-format-1, plugin-format-2, etc).
shared/
├── CMakeLists.txt
├── shared.cmake
│ .
project/
├── CMakeLists.txt
├── common/
├── common/CMakeLists.txt
│ common/common.cmake
│ .
When I'm developing and debbuging I want to avoid the hassle of compiling each lib individually so in debug mode I would usually include shared.pri and common.pri and when compiling the application, everything would compile smoothly, all toghether. I'm trying to replicate this include pattern using .cmake files.
#project.pro
include(../shared/shared.pri)
include(common/common.pri)
When I want to make a final and release version of the application I have a build script that compile the shared static lib, the common static lib and then links the different targets with this libs.
I have already created the CMakeLists.txt for the shared and common static libs. My problem now is how to replicate the pattern described above with cmake because when including the shared.cmake and common.cmake in the application CMakeLists.txt, the paths are not compatible. It seems that when you include(xxx.cmake), the path will always be relative to the parent CMakeLists.txt.
What would be the right way of achieving this with CMake? Is this even possible?
CodePudding user response:
Use add_subdirectory()
instead of include. For shared
, which is "out-of-tree", you'll also need to specify the binary directory (the place to put the generated and compiled files).
add_subdirectory(../shared shared)
add_subdirectory(common)
Any targets created in those subprojects will now be available for you to link with target_link_libraries
in your main project.
When you build your separate versions, consider using export()
and/or install(EXPORT)
to produce cmake files that you can include from your application projects instead of using add_subdirectory()
, so that can be the only change you need to make.