Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake project shared dependencies

Tags:

cmake

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.

like image 496
ShdNx Avatar asked Jul 14 '14 14:07

ShdNx


People also ask

Does CMake manage dependencies?

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.

Can CMake download dependencies?

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.

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How does CMake Find_library work?

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 .


1 Answers

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.

like image 141
Gombat Avatar answered Oct 19 '22 19:10

Gombat