<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 20, 2018 at 1:20 AM rmawatson rmawatson <<a href="mailto:rmawatson@hotmail.com">rmawatson@hotmail.com</a>> wrote:<br></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 style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<div style="margin:0px 0px 15px;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;clear:both;color:rgb(36,39,41);text-align:left;background-color:rgb(255,255,255)">
<span>I am trying to figure out exactly what this line is for in the cmake file of the github json project here -> <a href="https://github.com/nlohmann/json/blob/develop/CMakeLists.txt#L48" target="_blank">https://github.com/nlohmann/json/blob/develop/CMakeLists.txt#L48</a><br>
</span>
<div><br>
</div>
<div>add_library(${PROJECT_NAME}::${NLOHMANN_JSON_TARGET_NAME} ALIAS ${NLOHMANN_JSON_TARGET_NAME})<br>
</div>
<div><br>
</div>
<div>Specifically with this example, what does this allow in this cmake file that otherwise would not be possible?<br></div></div></div></div></blockquote><div>It is to make incorporating the project into a larger project via add_subdirectory() rather than via find_package() more convenient/consistent. The latter relies on an installed package, which will have been exported with the <font face="monospace, monospace">nlohmann_json::</font> namespace, so it will provide imported targets with names like <span style="font-family:monospace,monospace">nlohmann_json::</span><span style="font-family:monospace,monospace">nlohmann_json.</span> When incorporating the project via add_subdirectory() instead, nothing is exported, so targets like <span style="font-family:monospace,monospace">nlohmann_json::</span><span style="font-family:monospace,monospace">nlohmann_json</span> won't exist unless they are explicitly added as an ALIAS like in the example above.</div><div><br></div><div>In other words, ALIAS targets like this allow consuming projects to use the same target name (<span style="font-family:monospace,monospace">nlohmann_json::</span><span style="font-family:monospace,monospace">nlohmann_json</span>) regardless of how they bring the json project in as a dependency.</div><div><br></div><div><br></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 style="font-family:Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)"><div style="margin:0px 0px 15px;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;clear:both;color:rgb(36,39,41);text-align:left;background-color:rgb(255,255,255)"><div><br>
</div>
<div>The commit message where this line was added says,</div>
<div><br>
</div>
<div>"Enable target namespaces and build dir project config</div>
<div><br>
</div>
<div>CMake convention is to use a project namespace, i.e. Foo::, for imported<br>
</div>
<div>targets.  When multiple targets are imported from a project, this looks<br>
</div>
<div>like Foo::Bar1 Foo::Bar2, etc.  This adds the nlohmann_json:: namespace to<br>
</div>
<div>the exported target names.<br>
</div>
<div><br>
</div>
<div>This also allows the generated project config files to be used from the<br>
</div>
<span>build directory instead of just the install directory."</span><br>
</div>
<div style="margin:0px 0px 15px;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;clear:both;color:rgb(36,39,41);text-align:left;background-color:rgb(255,255,255)">
Removing this line appears to make no difference to any off the generated cmake files. I can still use find_package with the build directory in CMAKE_PREFIX-PATH with our without this line. Has something changed in cmake since this was added?</div></div></div></blockquote><div><br></div><div>The ALIAS won't change the exported files (because it isn't itself installed/exported), it is only for use directly by a parent project consuming it via add_subdirectory(). This is not a new feature, but it has perhaps been discussed more openly in the past year or two. When the <a href="https://cmake.org/cmake/help/latest/module/FetchContent.html">FetchContent</a> module was added in CMake 3.11, it also became much easier to incorporate a dependency via add_subdirectory().</div><div><br></div></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><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><br></div><div>New book released: <a href="https://crascit.com/professional-cmake/" target="_blank">Professional CMake: A Practical Guide</a><br></div></div></div></div></div></div></div></div></div></div></div></div></div>