Skip to content

CMake macro for loading static or shared exported library targets in a package config

License

Notifications You must be signed in to change notification settings

lepus2589/LoadStaticSharedTargets

Repository files navigation

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 and Ninja for building the project

Linux (Ubuntu)

Use your distribution's package manager to install the necessary packages:

$ sudo apt-get install cmake ninja-build

Mac OS X

You will need one of the community package managers for Mac OS X: Homebrew or MacPorts. For installing one of these, please refer to their respective installation instructions.

Homebrew

$ brew install cmake ninja

MacPorts

$ sudo port -vb install cmake-devel ninja

Windows

The easiest thing to do is using the Windows Subsystem for Linux (WSL) and follow the Linux instructions above.

Otherwise, install Git for Windows for version control and cygwin, which is a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. During the cygwin installation you'll be prompted to add additional packages. Search and select the following:

  • cmake and ninja for the build system

After cygwin finishes installing, use the cygwin terminal to start the build process.

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. Go to this directory and type cmake --list-presets. A list of available build configurations will be shown to you.

  3. For configuring the project, type cmake --preset ninja. This will populate the ./build directory with a CMake configuration tree. By default, the configured installation directory is ./install. You can specify a different install location by setting the CMAKE_INSTALL_PREFIX variable:

    $ cmake --preset ninja -D "CMAKE_INSTALL_PREFIX=/path/to/install/prefix"
  4. Now, you can build with cmake --build --preset ninja. You should see some informative terminal output.

  5. Finally, install the built artifacts to the configured install prefix with cmake --build --preset ninja --target install. If the configured install prefix is a system-wide location (like /usr/local), installing 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.5.2
    SYSTEM
    FIND_PACKAGE_ARGS 1.5.2 CONFIG NAMES LoadStaticSharedTargets
)
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. To help CMake discover an installed LoadStaticSharedTargets package, call CMake for the other project like this:

$ cmake -B ./build -D "CMAKE_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.

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.

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.

About

CMake macro for loading static or shared exported library targets in a package config

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages