Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • v2.0.0 protected
  • v1.9.0 protected
  • v1.8.0 protected
  • v1.7.0 protected
  • v1.6.0 protected
  • v1.5.2 protected
  • v1.5.1 protected
  • v1.5.0 protected
  • v1.4.1 protected
  • v1.4 protected
  • v1.4.0 protected
  • v1.3 protected
  • v1.3.0 protected
  • v1.2 protected
  • v1.2.0 protected
  • v1.1 protected
  • v1.1.0 protected
  • v1.0 protected
  • v1.0.0 protected
20 results

loadstaticsharedtargets

  • Clone with SSH
  • Clone with HTTPS
  • user avatar
    Tim Kaune authored
    LoadStaticSharedTargets is completely build type agnostic.
    06956657
    History

    LoadStaticSharedTargets

    This CMake module contains a macro for loading static or shared exported library targets in a CMake package config file. Static or shared targets can be explicitly (via COMPONENTS) or implicitly (via BUILD_SHARED_LIBS) imported with the find_package() command.

    Prerequisites

    What you need:

    • Git (optional) for getting the code and contributing to the project
    • CMake for building the project

    How to build

    1. Clone the git repository or download the source code archive and unpack it to an arbitrary directory (e.g. load-static-shared-targets).
    2. For building the module, type cmake --preset default. This will populate the build directory with a CMake build tree.
    3. Now, you can build with cmake --build ./build. You should see some informative terminal output.
    4. Finally, install the built artifacts to the bin folder with cmake --install ./build --prefix ./bin. You can also specify a different install prefix, e. g. if you want to install system-wide: cmake --install ./build --prefix /usr/local (might require sudo).

    Now, the LoadStaticSharedTargets module is installed and you can use it in other projects. The installed LoadStaticSharedTargets package is discoverable by CMake as LoadStaticSharedTargets. In the CMakeLists.txt of the other project, do the following:

    include(FetchContent)
    
    FetchContent_Declare(
        LoadStaticSharedTargets
        GIT_REPOSITORY "https://github.com/lepus2589/LoadStaticSharedTargets.git"
        GIT_TAG v1.4
        FIND_PACKAGE_ARGS NAMES LoadStaticSharedTargets CONFIG
    )
    set(LoadStaticSharedTargets_INCLUDE_PACKAGING TRUE)
    FetchContent_MakeAvailable(LoadStaticSharedTargets)

    This discovers an installed version of the LoadStaticSharedTargets module or adds it to the other project's install targets.

    In the other project's package config CMake file, you can now use the module like this:

    find_package(LoadStaticSharedTargets REQUIRED CONFIG)
    include(LoadStaticSharedTargets)
    
    load_static_shared_targets(
        STATIC_TARGETS
        "${CMAKE_CURRENT_LIST_DIR}/YourProject_Targets-static.cmake"
        SHARED_TARGETS
        "${CMAKE_CURRENT_LIST_DIR}/YourProject_Targets-shared.cmake"
    )

    This adds the LoadStaticSharedTargets module to the CMAKE_MODULE_PATH variable which enables the include by name only.

    Then, to help CMake discover the LoadStaticSharedTargets package, call CMake for the other project like this:

    $ cmake -B ./build -DCMAKE_PREFIX_PATH=/path/to/LoadStaticSharedTargets/install/prefix

    Replace the path to the LoadStaticSharedTargets install prefix with the actual path on your system! If LoadStaticSharedTargets is installed in a proper system-wide location, the CMAKE_PREFIX_PATH shouldn't be necessary.

    Further Reading

    The idea for this code was taken from @alexreinking's blog post: Building a Dual Shared and Static Library with CMake and the associated example repository.

    Information on project structure and usage of CMake library packages using this approach in other projects can be found there.