I have:
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.
Any complex software will have its dependencies – be it system API calls or other libraries calls either statically or dynamically linked to it. As a build system generator CMake will help you manage these dependencies in the most natural way possible.
Dependencies do not necessarily have to be pre-built in order to use them with CMake. They can be built from sources as part of the main project. The FetchContent module provides functionality to download content (typically sources, but can be anything) and add it to the main project if the dependency also uses CMake.
Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.
find_library tells CMake that we're looking for a library and once we have found it, to store the path to it in the variable LIBIMAGEPIPELINE_LIBRARY . Likely filenames are passed with NAMES LibImagePipeline . It is good practice to pass the names without the file extension like .
An additional answer for others:
I got this error because of a merge "error". In a larger project, after merging, a CMakeLists.txt called "add_subdirectory" twice on the same subdirectory. It causes the same error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With