Simple find_package config file example

Don’t write a config yourself; use CMake’s export command. It’s too broad to cover here in its entirety, but here’s a modified example from one of my projects:

install(TARGETS
        your_target
    EXPORT YourPackageConfig
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
export(TARGETS
        your_target
    NAMESPACE YourPackage::
    FILE "${CMAKE_CURRENT_BINARY_DIR}/YourPackageConfig.cmake"
)
install(EXPORT
        YourPackageConfig
    DESTINATION "${CMAKE_INSTALL_DATADIR}/YourPackage/cmake"
    NAMESPACE YourPackage::
)

This will create the config file for you, so other projects can use it via find_package.

find_package(YourPackage REQUIRED)
target_link_libraries(foo YouprPackage::your_target)

This handles the IMPORTED targets automatically, and also lets you embed compiler flags, include paths, library dependencies, and even which files are part of your interface (basically, anything that falls under the INTERFACE properties).

Leave a Comment