3

I am developing an application and have separated some functionality into a second repository. I want to

  1. Make the library repo a git submodule since both will be developed side-by-side
  2. Include the library as a dependency in my CMakeLists without installing it system wide.

So I have a layout like this:

. CMakeLists.txt
|_ src
|_ include
|_ third_party
   |_ my_library
      |_ CMakeLists.txt
      |_ src
      |_ include

Now the CMakeLists.txt in my_library creates some lib with add_library(), and it also has an install target which copies libmy_library.a to my_library/lib.

I would like to depend on this library in my toplevel CMakeLists.txt. So what I need is for the my_library CMake project to be built and installed before I try to reference the library for my own targets. I tried using ExternalProject_Add() for this like so:

./CMakeLists.txt

set(MY_LIB_DIR ${CMAKE_SOURCE_DIR}/third_party/my_library)

ExternalProject_Add(my_library
    SOURCE_DIR ${MY_LIB_DIR}
    UPDATE_DISCONNECTED TRUE 
    BUILD_IN_SOURCE FALSE
    BINARY_DIR ${MY_LIB_DIR}/build
)

list(APPEND CMAKE_PREFIX_PATH ${MY_LIB_DIR}/lib)

# find compiled lib
find_library(MY_LIB_LIBRARY my_library)

add_executable(main ${SOURCES})
add_dependencies(main my_library)  
target_link_libraries(main ${MY_LIB_LIBRARY})

However, this does not work since at the time cmake reads the configuration, libmy_library.a has not been built yet.

The question thus: How can I make CMake build and install an external project so I can list install artifacts as dependencies?

It has been suggested this is a duplicate, but the linked answer builds the external project inside the current project, which I find undesirable.

Claudio
  • 10,614
  • 4
  • 31
  • 71
oarfish
  • 4,116
  • 4
  • 37
  • 66
  • Is there anything specific to your setup that prevents you from just using `add_subdirectory(third_party/my_library)` ? –  May 16 '19 at 20:04
  • Unlike, say, Maven, CMake is not a dependency management tool AFAIK. It assumes that all dependencies are already present and can be located. –  May 16 '19 at 20:06
  • @Frank One reason was that both projects use `gtest` and thus there is a duplicate target. – oarfish May 17 '19 at 06:03

0 Answers0