<div dir="ltr">Thanks for trying out the new FetchContent module. Comments on your question interspersed below.<div><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 12, 2018 at 11:02 AM, Saad Khattak <span dir="ltr"><<a href="mailto:saadrustam@gmail.com" target="_blank">saadrustam@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>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.</div><div><br></div><div>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.</div></div></blockquote><div><br></div><div>This is indeed how it should work. After you've called add_subdirectory(), the targets defined by the project being added should be immediately available just like any other target your own project would define.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>Here's my CMakeLists.txt<br></div><div>``````````````````````````````<wbr>``````````````````````````````<wbr>``</div><div><div>cmake_minimum_required(VERSION 3.5)</div></div></div></blockquote><div><br></div><div><div class="gmail_quote">Your minimum CMake version will be 3.11 if you want to use FetchContent, so consider specifying that as the minimum version here.</div></div><div><font face="monospace, monospace"><br></font></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>project(testProj)<br></div><div><br></div><div>include(FetchContent)</div><div><br></div><div>FetchContent_Declare(</div><div> Catch2</div><div> GIT_REPOSITORY "<a href="https://github.com/catchorg/Catch2" target="_blank">https://github.com/catchorg/<wbr>Catch2</a>"</div><div> TEST_COMMAND ""</div><div> )</div></div></div></blockquote><div><br></div><div>As stated in the docs, TEST_COMMAND will be ignored, so don't set it here. FetchContent only downloads, it doesn't build (you're in control of that, see further comments below). The ExternalProject_Add() docs also recommend you set GIT_TAG to the specific git hash you want to use (apart from being more robust, it saves having to contact the remote each time CMake is run after it has been cloned). FetchContent delegates all downloading to ExternalProject, so the comments apply here as well.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><br></div><div>FetchContent_GetProperties(<wbr>catch)</div></div></div></blockquote><div><br></div><div>This needs to be the same as the first argument to FetchContent_Declare() above, i.e. Catch2</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>if(NOT Catch2_POPULATED)</div><div> FetchContent_Populate(Catch2)</div><div> add_subdirectory(${Catch2_<wbr>SOURCE_DIR} ${Catch2_BINARY_DIR})</div></div></div></blockquote><div><br></div><div>The documentation for the FetchContent_Populate() function state that the variable names use lowercased names of the content name (i.e. catch2_SOURCE_DIR rather than Catch2_SOURCE_DIR), so both of these variables used here will be empty. I'm a little surprised CMake didn't complain about that, it does for me when I tested this just now. Same for the variable name used in the if test (i.e. use catch2_POPULATED rather than Catch2_POPULATED).</div><div><br></div><div>The reason for the lowercasing is that it was found during the 2 years or so when we were dog-fooding this module that while people usually got the name right, there would be variations in upper/lowercase which made things less reliable.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>endif()</div><div><br></div><div>add_executable(testExe main.cpp)</div><div><br></div><div>target_link_libraries(testExe Catch2)</div></div></div></blockquote><div><br></div><div>If the Catch2 project defines a library called Catch2, then this line should work fine once you fix the above problems. I think you want Catch rather than Catch2 though.</div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>``````````````````````````````<wbr>``````````````````````````````<wbr>``<br></div><div><br></div><div>CMake populates Catch2 with Catch2-NOTFOUND.</div><div><br></div><div>So, my question is, how do I link against projects added through FetchContent?</div></div></blockquote><div><br></div><div><br></div><div><br></div></div><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div></div></div>