I have a CMake Project Foobar that contains a subdirectory examples that can also be used as a stand-alone CMake build. To do that this sub-directory performs a find_package(Foobar) and uses the exported targets. Foobar provides a FoobarConfig.cmake, FoobarConfigVersion.cmake, and a FoobarExports.cmake and can be used without a FindModule.
The code looks roughly like this: ### Top-Level CMakeLists.txt ### cmake_minimum_required(VERSION 3.0.0) project(Foobar) add_library(X SHARED ${my_sources}) install(TARGETS X EXPORT FoobarExports LIBRARY DESTINATION ${my_install_destination}) install(EXPORT FoobarExports DESTINATION ${my_install_destination}) # Create the FoobarExports.cmake for the local build tree export(EXPORT FoobarExports) # the problematic command # Setup FoobarConfig.cmake etc # FoobarConfig.cmake includes FoobarExports.cmake # ... # Force find_package to FOOBAR_DIR option(BUILD_EXAMPLES "Build examples" ON) if(BUILD_EXAMPLES) set(FOOBAR_DIR "${CMAKE_BINARY_DIR}") add_subdirectory(examples) endif() ### examples/CMakeLists.txt ### cmake_minimum_required(VERSION 3.0.0) project(FoobarExamples) # Uses FOOBAR_DIR set above find_package(Foobar NO_MODULE REQUIRED) add_executable(my_exe ${some_sources}) # Use X from Foobar target_link_library(my_exe X) The problem is that export(EXPORT FoobarExports) will only create the FoobarExports.cmake file at the end of generation time to make sure it has the complete FoobarExports export set. So this will fail: cmake . -DBUILD_EXAMPLES=ON # Error: FoobarExports.cmake not found What works, however, is: cmake . cmake . -DBUILD_EXAMPLES=ON # rerun cmake with changed cache variable How can I either force the FoobarExports.cmake file to be written at the time of the call to export or force CMake to run twice, if the file has not been created yet? I also posted this question on StackOverflow [1], but realized to late that it might be a bit too specific and a better place would be this list. Thanks, Philipp [1] : http://stackoverflow.com/questions/24673331/requiring-exported-cmake-targets-within-the-exporting-build-tree -- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: http://public.kitware.com/mailman/listinfo/cmake