Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake find_package dependency on subproject

Tags:

cmake

I have the following directory layout:

main_folder
 + static_lib1
 + executable
  • Both 'static_lib1' and 'executable' have a full CMakeLists so that they can be built independently.
  • The 'executable' depends on 'static_lib1'. It uses find_package() to locate 'static_lib1'.
  • The main_folder contains a CMakeLists that includes both 'static_lib1' and 'executable' via add_subdirectory for conveniently building the whole project in one go.

Everything works fine if I manually build 'static_lib1' and then 'executable'. But when running the CMakeLists from the main folder, I get an error because find_package is unable to find the library files from 'static_lib1' which have not yet been built.

How can I resolve this while keeping the CMakeLists files separate (i.e. without including the static_lib's CMakeLists from the executable's CMakeLists)?

like image 918
ComicSansMS Avatar asked Nov 24 '11 17:11

ComicSansMS


1 Answers

In executable's CMakeLists.txt you can check if you are building stand-alone or as part of project:

if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
  # stand-alone build
  find_package(static_lib1)
else()
  include_directories(../static_lib1)
  link_directories(../static_lib1)
  ...
  target_link_libraries(executable static_lib1)
endif()
like image 52
arrowd Avatar answered Oct 21 '22 03:10

arrowd