[CMake] How to depend on external cmake projects?

Tamás Kenéz tamas.kenez at gmail.com
Wed Aug 19 17:56:18 EDT 2015


Robert,

1. If it's a library you are not modifying, only linking to, then you don't
need its targets in your projects. You just need to clone, build and
install it before running cmake for your actual project. It can be
accomplished in various ways:

- you can clone it as a submodule or manually by a script (shell or CMake
script or CMakeLists.txt)
- write a shell script that builds and installs it by calling `cmake` and
`cmake --build`
- write a super-project CMakeLists.txt which builds and installs it with
execute_process(${CMAKE_COMMAND} ...) (or ExternalProject, but it's a bit
cumbersome because you need to build the super-project to install the
dependency)
- build and install your dependency with execute_process(${CMAKE_COMMAND}
...) in your project's CMakeLists.txt (that is, in cmake configure-time)

2. If it's a library you will modify then:

- if it's not a submodule, you need to clone it either in a shell script or
in the super-project or in your project's CMakeLists
- use add_library to add it into your project

In first case you will need to use packages, or more precisely,
config-modules but I would not complicate things by creating
config-packages for the build tree. Create regular config modules
(install(EXPORT ...) and install the projects.

Tamas

On Sun, Aug 16, 2015 at 3:32 PM, Robert Dailey <rcdailey.lists at gmail.com>
wrote:

> There are certain repositories on Github I'd like to pull in as a
> dependency. These repositories already use CMake to build their source
> code. Based on personal experience and reading material online, there
> are a couple of different options:
>
> 1. Pull down the repository as a submodule to my own git repository.
> Then call add_subdirectory() on it to make it part of the build,
> allowing me to refer to those targets as dependencies in my own
> targets.
>
> 2. Use ExternalProject_Add(). This seems like legacy, I'm not sure if
> I should use this. It also seems very verbose and boilerplate.
>
> 3. Use packages. The [CMake package documentation][1] does not go into
> great detail on support for both installed versions and build tree for
> a single package. Does this require two scripts? Can it share a single
> one? Where are the examples?
>
> How do I accomplish this?
>
> [1]:
> http://www.cmake.org/cmake/help/v3.3/manual/cmake-packages.7.html#creating-a-package-configuration-file-for-the-build-tree
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150819/2d1b81fd/attachment-0001.html>


More information about the CMake mailing list