<div dir="ltr"><div>I have a similarly organized project and it works without any of the <font face="monospace, monospace">find_package </font>and the <font face="monospace, monospace">export </font>commands.</div><div><br></div><div>If you do include all of the CMakeFiles.txt files using <font face="monospace, monospace">add_directory</font>, then it should be enough to:</div><div><br></div><div><div>In my_common_libs/common_lib1/CMakeLists.txt:</div><div><span style="font-family:monospace,monospace">    add_library(common_lib1 ...)</span><br></div></div><div><br></div><div><div>In executable1/CMakeLists.text:</div><div><span style="font-family:monospace,monospace">    add_executable(executable1 ...)</span><br></div><div><span style="font-family:monospace,monospace">    target_link_libraries(</span><span style="font-family:monospace,monospace">executable1 </span><span style="font-family:monospace,monospace">common_lib1 )</span></div></div><div><span style="font-family:monospace,monospace"><br></span></div><div><font face="arial, helvetica, sans-serif">This way you don't need to worry about ordering your </font><font face="monospace, monospace">add_directory </font><font face="arial, helvetica, sans-serif">commands as all the link dependencies will be ordered by CMake.</font></div><div><br></div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">-- <br>Rafael Vargas</div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Em qua, 10 de abr de 2019 às 18:08, Timothy Wrona <<a href="mailto:tjwrona1992@gmail.com">tjwrona1992@gmail.com</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi fellow CMake programmers! I've got a bit of a tricky situation and I wonder if anyone has ever dealt with this before.</div><div><br></div>I am trying to configure a project that has a structure as follows<div><br></div><div><font face="monospace, monospace">myproject/</font></div><div><font face="monospace, monospace">    executable1/  <-- depends on common_lib1</font></div><div><font face="monospace, monospace">    my_common_libs/</font></div><div><font face="monospace, monospace">        common_lib1/</font></div><div><font face="monospace, monospace">        common_lib2/</font></div><div><br></div><div>Note: Before I begin - this is a legacy system and is MUCH larger than what I am showing here so restructuring the whole project is not really an option. Let me begin by describing the CMakeLists.txt structure.</div><div><br></div><div>In myproject/CMakeLists.txt subdirectories are added as follows:</div><div><br></div><div><font face="monospace, monospace">file(GLOB entries *)</font></div><div><font face="monospace, monospace">foreach(entry ${entries})</font></div><div><font face="monospace, monospace">  if (IS_DIRECTORY ${entry} AND EXISTS ${entry}/CMakeLists.txt)</font></div><div><font face="monospace, monospace">    add_subdirectory(${entry})</font></div><div><font face="monospace, monospace">  endif()</font></div><div><font face="monospace, monospace">endforeach()</font></div><div><br></div><div>my_common_libs/CMakeLists.txt does the same...</div><div><br></div><div>Then my_common_libs/common_lib1/CMakeLists.txt does what is necessary to define the library and export the target to the local package registry:</div><div><br></div><div><font face="monospace, monospace">add_library(common_lib1 ...)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">export(TARGETS common_lib1 NAMESPACE common_lib1:: FILE common_lib1-targets.cmake)</font></div><div><font face="monospace, monospace">file(COPY common_lib1-config.cmake DESTINATION ${CMAKE_CURRENT_BINARY_DIR})</font></div><div><font face="monospace, monospace">export(PACKAGE common_lib1)</font></div><div><br></div><div>my_common_libs/common_lib2/CMakeLists.txt does the same and both have "common_libx-config.cmake" files that properly load the corresponding "-targets.cmake" files.</div><div><br></div><div>In executable1/CMakeLists.text I do what is necessary to find the package and use it:</div><div><br></div><div><font face="monospace, monospace">add_executable(executable1 ...)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">find_package(common_lib1 REQUIRED) <-- this is where I am having the problem</font></div><div><font face="monospace, monospace">target_link_libraries(executable1</font></div><div><font face="monospace, monospace">    common_lib1::common_lib1</font></div><div><font face="monospace, monospace">)</font></div><div><br></div><div>Now this is where things get hairy...</div><div><br></div><div>If I go into common_lib1 and manually build that first, it puts it in the package registry and then I can go to "executable1" and build that and it finds the package, and builds, and links fine.</div><div>BUT... if I clean the project and then try to build it from the top level it immediately fails and says it can't locate the package "common_lib1".</div><div><br></div><div>I understand I could probably replace the GLOB commands with explicit "add_subdirectory" calls and then carefully reorder all of the "add_subdirectory" commands to add things in dependency order, but that would be really tedious with how large the project is and is really not the way the whole rest of the system is done... It feels somewhat brittle because then I am essentially declaring the dependencies from outside of the modules themselves.</div><div><br></div><div>Is there any way to force CMake to do all of the "export" commands up front to essentially initialize the package registry so it will be aware of common_lib1 when I compile from the "myproject" level? Or is there another solution I haven't even considered that could solve this same problem?</div><div><br></div><div>Thanks,<br></div><div>Tim</div></div>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>