Created
February 15, 2023 16:48
-
-
Save alexreinking/51f9f06a7cef474a3f8bbe2aed20af9c to your computer and use it in GitHub Desktop.
Basic executable packaging/export example in CMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.25) | |
project(example VERSION 0.1.0) | |
add_executable(example main.cpp) | |
add_executable(example::example ALIAS example) | |
include(GNUInstallDirs) | |
include(CMakePackageConfigHelpers) | |
set(EXAMPLE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/cmake/Example" | |
CACHE STRING "Install path for example CMake package files") | |
configure_package_config_file( | |
ExampleConfig.cmake.in "${EXAMPLE_INSTALL_CMAKEDIR}/ExampleConfig.cmake" | |
INSTALL_DESTINATION "${EXAMPLE_INSTALL_CMAKEDIR}" | |
NO_SET_AND_CHECK_MACRO | |
NO_CHECK_REQUIRED_COMPONENTS_MACRO | |
) | |
write_basic_package_version_file( | |
"${EXAMPLE_INSTALL_CMAKEDIR}/ExampleConfigVersion.cmake" | |
VERSION ${example_VERSION} | |
COMPATIBILITY SameMajorVersion | |
) | |
install( | |
FILES "${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_INSTALL_CMAKEDIR}/ExampleConfig.cmake" | |
"${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_INSTALL_CMAKEDIR}/ExampleConfigVersion.cmake" | |
DESTINATION "${EXAMPLE_INSTALL_CMAKEDIR}" | |
) | |
install(TARGETS example EXPORT example-targets) | |
install( | |
EXPORT example-targets | |
NAMESPACE example:: | |
FILE ExampleTargets.cmake | |
DESTINATION "${EXAMPLE_INSTALL_CMAKEDIR}" | |
) | |
export( | |
EXPORT example-targets | |
NAMESPACE example:: | |
FILE "${EXAMPLE_INSTALL_CMAKEDIR}/ExampleTargets.cmake" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PACKAGE_INIT@ | |
include("${CMAKE_CURRENT_LIST_DIR}/ExampleTargets.cmake") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main () { return 0; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eux -o pipefail | |
rm -rf build _local usepkg | |
cmake -G Ninja -S . -B build/main -DCMAKE_BUILD_TYPE=Release | |
cmake --build build/main | |
cmake --install build/main --prefix _local | |
mkdir usepkg | |
cp usepkg.cmake usepkg/CMakeLists.txt | |
cmake -G Ninja -S usepkg -B build/usepkg -DCMAKE_PREFIX_PATH=$PWD/build/main --fresh | |
cmake -G Ninja -S usepkg -B build/usepkg -DCMAKE_PREFIX_PATH=$PWD/_local --fresh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.25) | |
project(use-pkg) | |
find_package(Example REQUIRED) | |
include(CMakePrintHelpers) | |
cmake_print_properties( | |
TARGETS example::example | |
PROPERTIES IMPORTED_LOCATION_RELEASE | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment