[CMake] How to link against projects added through FetchContent

Saad Khattak saadrustam at gmail.com
Sun Mar 11 20:52:28 EDT 2018


I managed to find a solution myself:

``````````````````````````````````````````````````````````````
cmake_minimum_required(VERSION 3.11)

project(testProj)

include(FetchContent)

FetchContent_Declare(
  Catch2
  GIT_REPOSITORY "https://github.com/catchorg/Catch2"
  )

FetchContent_GetProperties(Catch2) #mispelled name in original post
if(NOT Catch2_POPULATED)
  FetchContent_Populate(Catch2)
  message(STATUS "Catch source dir: ${catch2_SOURCE_DIR}")
  message(STATUS "Catch binary dir: ${catch2_BINARY_DIR}")
  add_subdirectory(${catch2_SOURCE_DIR} ${catch2_BINARY_DIR}) #can be case
insensitive
endif()

add_executable(testExe
  main.cpp
  )

message(STATUS "Catch include dir: ${catch2_SOURCE_DIR}/include")

target_link_libraries(testExe Catch) #name of library to link is case
sensitive!
``````````````````````````````````````````````````````````````

On Sun, Mar 11, 2018 at 8:02 PM Saad Khattak <saadrustam at gmail.com> wrote:

> Hi,
>
> I would like to know how to use the FetchContent properly so that I can
> link against downloaded (CMake enabled) projects. I have looked at the
> CMake docs, which although are quite thorough, almost always fail to list a
> complete example which is incredibly crucial to get up and running quickly.
>
> With ExternalProject_Add, we use add_dependencies(...) but that doesn't
> seem to be the case for FetchContent. Since I can immediately call
> add_subdirectory(...), I assumed that I can simply link to the library. But
> that doesn't seem to do anything.
>
> Here's my CMakeLists.txt
> ``````````````````````````````````````````````````````````````
> cmake_minimum_required(VERSION 3.5)
> project(testProj)
>
> include(FetchContent)
>
> FetchContent_Declare(
>   Catch2
>   GIT_REPOSITORY "https://github.com/catchorg/Catch2"
>   TEST_COMMAND ""
>   )
>
> FetchContent_GetProperties(catch)
> if(NOT Catch2_POPULATED)
>   FetchContent_Populate(Catch2)
>   add_subdirectory(${Catch2_SOURCE_DIR} ${Catch2_BINARY_DIR})
> endif()
>
> add_executable(testExe main.cpp)
>
> target_link_libraries(testExe Catch2)
> ``````````````````````````````````````````````````````````````
>
> CMake populates Catch2 with Catch2-NOTFOUND.
>
> So, my question is, how do I link against projects added through
> FetchContent?
>
> - Saad
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180312/99e6563e/attachment.html>


More information about the CMake mailing list