Steve.Q Steve.Q - 1 month ago 15
C++ Question

Linking against an ExternalProject_add dependency in CMAKE

I'm getting this ninja build error below while running Ninja. My CMAKE build command is

cmake -G"Ninja" -DCMAKE_BUILD_TYPE=Release
.

ninja: error: 'ext_deps/api/src/ext_api/build/src/lib/libapi.a', needed by 'Project', missing and no known rule to make it


Let's say my project consists of an API (downloaded via CMAKE from GitHub) and the implementation (the Project).

The layout would look like:
Project/
-- build/
-- cmake/modules
----- ExternalDep.cmake
----- FindAPI.cmake
-- CMakeLists.txt
-- src/
---- CMakeLists.txt
-- include/


Let's say that in the top-level
CMakeLists.txt
I do the usual business of setting build settings, CXX flags, et cetera, and then I call
include(ExternalDep)
, which checks if the "API" library is in the user's system (if not it is downloaded via CMAKE).
In
src/CMakeLists.txt
I try to link against the API library using a

target_link_libraries(${PROJECT_NAME} PRIVATE ${API_LIBRARY})


The first issue I'm having is that before the "API" library can even be downloaded and built, I get the ninja build error I posted above. I'm positive the
ExternalDep.cmake
is included before I try to add the Project executable and link against the "API" library.

Here's a simplified version of
ExternalDep.cmake
:

set(EXT_DEPS_PREFIX "ext_deps")

ExternalProject_Add(ext_lib
GIT_REPOSITORY "https://github.com/fake/api.git"
GIT_TAG "master"
PREFIX "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api"
TMP_DIR "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api-tmp"
STAMP_DIR "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api-stamp"
CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release
SOURCE_DIR "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api/ext_api"
BINARY_DIR "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api/ext_api-build"
BUILD_ALWAYS true
TEST_COMMAND "")

add_dependencies(ext_projects ext_api)

set(API_LIBRARY "${CMAKE_BINARY_DIR}/${EXT_DEPS_PREFIX}/api/ext_api-build/src/lib/libapi.a")

Answer

I was able to solve this by generating Unix Makefiles instead of Ninja. I'm still not exactly sure if this was the singular issue but it was definitely one of the issues.