Unknown CMake command "ExternalProject_Add"
Asked Answered
E

2

35

I have following CMakeLists.txt file:

cmake_minimum_required (VERSION 3.2 FATAL_ERROR)

project (utils VERSION 1.0.0 LANGUAGES CXX)

ExternalProject_Add(json-c
    GIT_REPOSITORY "https://github.com/json-c/json-c.git"
    UPDATE_COMMAND git pull "https://github.com/json-c/json-c.git"

    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/SDL_image/./configure
                      --prefix=${SDL_INSTALL_DIR}

    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/json-c

    INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}
    )

I want to add json-c library to my project, but when I run cmake I'm getting error: Unknown CMake command "ExternalProject_Add". My CMake version is 3.6.2 on OS X

Elephantine answered 12/1, 2017 at 16:40 Comment(1)
You missed include(ExternalProject)?!Greatest
D
38

The required module should be part of your cmake installation. But you have to include it into your project with:

include(ExternalProject)

before the call of externalproject_add(YOUR STUFF HERE). See Modules

There are two include variants:

include(<MODULE_NAME_WITHOUT_.cmake>)
include(<FULL_PATH_TO_MODULE_WITH_.cmake>)

Examples:

include(gcovr)
include(${PROJECT_DIR}/cmake/gcovr.cmake)

Modules are plain cmake files and have to be included like your own module files (In case you have them).

The variable CMAKE_MODULE_PATH is a list of all directories where cmake is loading modules from. You can print out the current value with:

message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") 

Or you are using smart macros for that. See CMake

Denigrate answered 13/1, 2017 at 7:36 Comment(1)
It doesn't work for me : include could not find load file: ExternalProject.cmake. But it works with exactly include(ExternalProject) as in the answer from @Tsyvarev.Farmhouse
I
49

While it is not directly written in documentation pages for versions prior to v3.14, CMake functions described under cmake-modules section requires including specific module.

As function ExternalProject_Add is described in the documentation page titled as "ExternalProject", you need to use

include(ExternalProject)

before using it.


Same strategy works for any other modules except Find<name> ones. Those modules are used via

find_package(<name>)
Inculpable answered 16/1, 2017 at 7:6 Comment(3)
For anyone confused, ExternalProject is not a variable, such as "my_c_project". You have to literally include 'ExternalProject'Comportment
"While it is not directly written in documentation pages" - oh, excellent, I'm just loving CMake more and more. It's so nice when the developers can't be bothered to include required steps in their documentation...Piffle
@Piffle These modules are delivered together with cmake but plain cmake files and have to be included into the CMakeLists.txt like every other external cmake file. And starting with v3.14 somebody extended the documentation.Denigrate
D
38

The required module should be part of your cmake installation. But you have to include it into your project with:

include(ExternalProject)

before the call of externalproject_add(YOUR STUFF HERE). See Modules

There are two include variants:

include(<MODULE_NAME_WITHOUT_.cmake>)
include(<FULL_PATH_TO_MODULE_WITH_.cmake>)

Examples:

include(gcovr)
include(${PROJECT_DIR}/cmake/gcovr.cmake)

Modules are plain cmake files and have to be included like your own module files (In case you have them).

The variable CMAKE_MODULE_PATH is a list of all directories where cmake is loading modules from. You can print out the current value with:

message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}") 

Or you are using smart macros for that. See CMake

Denigrate answered 13/1, 2017 at 7:36 Comment(1)
It doesn't work for me : include could not find load file: ExternalProject.cmake. But it works with exactly include(ExternalProject) as in the answer from @Tsyvarev.Farmhouse

© 2022 - 2024 — McMap. All rights reserved.