スタンドアロンの CMake ビルドとしても使用できるFoobar
サブディレクトリを含むCMake プロジェクトがあります。examples
そのために、このサブディレクトリはfind_package(Foobar)
エクスポートされたターゲットを実行して使用します。、、および をFoobar
提供し、FindModule なしで使用できます。FoobarConfig.cmake
FoobarConfigVersion.cmake
FoobarExports.cmake
コードは大まかに次のようになります。
### 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)
問題は、生成時間の最後にexport(EXPORT FoobarExports)
のみFoobarExports.cmake
ファイルを作成して、完全なFoobarExports
エクスポート セットがあることを確認することです。
したがって、これは失敗します:
cmake . -DBUILD_EXAMPLES=ON
# Error: FoobarExports.cmake not found
ただし、機能するのは次のとおりです。
cmake .
cmake . -DBUILD_EXAMPLES=ON # rerun cmake with changed cache variable
ファイルがまだ作成されていない場合、FoobarExports.cmake
呼び出し時にファイルを強制的に書き込むか、 CMake を 2 回実行させるにはどうすればよいですか?export