I have:
- a shared library X, which is independent
- a shared library Y, using X
- an executable Z, which uses both X and Y
All of them have their own CMakeLists.txt, and can be configured and built independently.
However, I cannot make the CMakeLists.txt for the executable (Z) to work.
My approach has been this:
foreach(clib ${OWN_LIBS})
set(LIBS "${LIBS} ${clib}")
set(CLIB_DIR "${PROJECT_SOURCE_DIR}/../lib${clib}")
set(CLIB_BUILD_DIR "${CLIB_DIR}/build")
add_subdirectory("${CLIB_DIR}" "${CLIB_BUILD_DIR}")
include_directories("${CLIB_DIR}/incl")
link_directories("${CLIB_BUILD_DIR}")
endforeach(clib)
With OWN_LIBS being just "X" in project Y, and being "X Y" in project Z.
This works for project Y, but in project Z, I get:
CMake Error at ... (add_subdirectory): The binary directory
.../libX/build
is already used to build a source directory. It cannot be used to build source directory
.../libX
Specify a unique binary directory name.
I also tried trying to create a local build directory, so for e.g. there would be libY/build/deps-libX/ containing the configured and built library X (when used from Y), and Z having this for both X and Y. Unfortunately, next I ran into:
add_library cannot create target "X" because another target with the same name already exists. The existing target is a shared library created in source directory "libX". See documentation for policy CMP0002 for more details.
Using ExternalProject is not an option.