Issue
My folder structure is like that:
├── SubLibA
│ ├── CMakeLists.txt
│ ├── include
│ │ └── SubLibA.h
│ └── SubLibA.cpp
├── SubLibB
│ ├── CMakeLists.txt
│ ├── include
│ │ └── structs.h
│ └── SubLibB.cpp
└── SharedLib
├── CMakeLists.txt
├── include
│ └── SharedLib.h
├── SharedLib.cpp
└── SharedLib.h
My global CMakeLists.txt
looks like this:
add_subdirectory(SubLibA)
add_subdirectory(SubLibB)
add_subdirectory(SharedLib)
They all compile as static by default.
SharedLib
depends onSubLibB
that depends onSubLibA
.- The dependent libraries
SharedLib
andSubLibB
have:
#SubLibB
target_link_libraries(${PROJECT_NAME}
SubLibA::SubLibA
)
#SharedLib
target_link_libraries(${PROJECT_NAME}
SubLibB::SubLibB
)
Running cmake .. -DBUILD_SHARED_LIBS=ON
compiles all the three libs as shared library...
Since they are tightly dependent, I'd like to keep them in the same repository with a unique CMakeLists.txt that compiles them all at once. I want to use the power of Modern CMake with the least hard-coded file and custom files as possible to keep a straightforward maintenance.
Solution
Try setting the variable within cmake:
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(SubLibA)
add_subdirectory(SubLibB)
set(BUILD_SHARED_LIBS ON)
add_subdirectory(SharedLib)
set(BUILD_SHARED_LIBS OFF)
Answered By - KamilCuk