<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>