[CMake] ExternalProject_Add

Saad Khattak saadrustam at gmail.com
Sun Jan 8 16:49:52 EST 2017


Hello,

I have an external project glfw that I added to my project like this:

==========
include(ExternalProject)
ExternalProject_Add(glfw
  GIT_REPOSITORY "https://github.com/glfw/glfw.git"
  GIT_TAG "master"

  SOURCE_DIR "${CMAKE_SOURCE_DIR}/dep/glfw"
  CMAKE_ARGS -DGLFW_BUILD_DOCS=OFF -DGLFW_BUILD_TESTS=OFF
-DGLFW_BUILD_EXAMPLES=OFF
-DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}/install/glfw/
-DCMAKE_DEBUG_POSTFIX=_d

  TEST_COMMAND ""
  )
set(GLFW_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/install/glfw/include/")
set(GLFW_LIBRARY_DIR "${CMAKE_SOURCE_DIR}/install/glfw/lib/")
==========

Then I include it in my project like so:

==========
find_library(GLFW_LIB_D  glfw3_d ${GLFW_LIBRARY_DIR})
find_library(GLFW_LIB    glfw3   ${GLFW_LIBRARY_DIR})

include_directories(${GLFW_INCLUDE_DIR})

add_executable(vk_test
  src/vulkan_test.cpp
  )
target_link_libraries(vk_test debug ${GLFW_LIB_D} optimized ${GLFW_LIB})
add_dependencies(vk_test glfw)
==========

As you can see, I depend on the libraries compiled by the external project
glfw. Unfortunately, when I first configure the project, CMake complains
that it cannot find the libraries specified by ${GLFW_LIB_D} and
${GLFW_LIB} variables.

Of course, this is because CMake did not begin cloning, configuring and
building the glfw project. That only happens AFTER my project has been
configured and starts building. This becomes a chicken and the egg problem.

Currently, my solution is to add dummy .lib files so that I can at least
configure and generate my project. My question is, am I approaching this
problem in the wrong way? If yes, what is the correct way to add a
dependency to an external project and have it clone/configure/build BEFORE
the "find_library" call?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170108/5bd3f023/attachment.html>


More information about the CMake mailing list