3

I'm trying to include external libraries to a cmake project through ExternalProject_Add. To try out this feature I've created a minimal working example that involves adding pugixml to the project with ExternalProject_Add. However, I'm having problems finding a way to add the library's header files from the external project's local installation (i.e., pugixml's headers) in the project's include path.

The project tree of the minimal working example is organized as follows:

.
├── build
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    └── main.cpp

In this project tree, build refers to the build directory and the path where cmake is called to generate the build.

The contents of ./CMakeLists.txt are as follows:

cmake_minimum_required(VERSION 3.0)

include(ExternalProject)

ExternalProject_Add(pugixml
        GIT_REPOSITORY https://github.com/zeux/pugixml.git
        INSTALL_DIR ${PROJECT_BINARY_DIR}/extern_install/pugixml
        CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
)

add_subdirectory(src)

In the above example I've added pugixml as an external project that is to be installed within the project's binary dir, whose file will be used by an executable stored in ./src. Thus, the contents of ./src/CMakeLists.txt are:

project(foo)

add_executable(foo main.cpp)
target_link_libraries(foo ${pugixml_LIBRARIES})
include_directories(${pugixml_INCLUDE_DIR}) # this part doesn't work

This is precisely the part I'm having problems. I've assumed that once the external project was added and installed that ExternalProject_Add would have defined some convenience libraries to help refer to library files and include directories. However, that doesn't work. Does anyone know what's the right way of using ExternalProject_Add to include external libraries?

RAM
  • 2,257
  • 2
  • 19
  • 41
  • Possible duplicate of [CMake - linking to library downloaded from ExternalProject\_add()](https://stackoverflow.com/questions/6351609/cmake-linking-to-library-downloaded-from-externalproject-add) – Tsyvarev Aug 11 '18 at 19:27

1 Answers1

3

Unfortunately this will not work at all. The build of the external project is done at build time not during CMake configure/generate. The hint is The ExternalProject_Add function creates a custom target to drive download, update/patch, configure, build, install and test steps of an external project: (from CMake)

Hence You will have to define all the variables yourself. See also: CMake - linking to library downloaded from ExternalProject_add() (I'd actually mark this as a duplicate of the linked question as the problem is the same)

BTW: I do also dislike the way this was done. This way one cannot simply use find_package etc.

The intended way to use this is a "super-build project": Define 1 CMakeLists where you only have ExternalProject_Adds for all dependencies AND your project. Then it will work with find_package.

Flamefire
  • 5,313
  • 3
  • 35
  • 70