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 imported
explicitly (via COMPONENTS
) or implicitly (via BUILD_SHARED_LIBS
) 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 -v install cmake 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
andninja
for the build system
After cygwin
finishes installing, use the cygwin terminal to start the build
process.
How to build
-
Clone the git repository or download the source code archive and unpack it to an arbitrary directory (e.g.
load-static-shared-targets
). -
Go to this directory and type
cp ./CMakeUserPresets.template.json ./CMakeUserPresets.json
. -
Next, type
cmake --workflow --list-presets
. A list of available build workflows will be shown to you. -
For configuring, building and installing the project, type
cmake --workflow --preset user-install
. This will populate the./build
directory with a CMake configuration tree, execute the build and install the project. By default, the configured installation directory is./install
.You can specify a different install location by setting the
CMAKE_INSTALL_PREFIX
variable in the"user-config"
preset in your./CMakeUserPresets.json
:"cacheVariables": { "CMAKE_INSTALL_PREFIX": "$env{HOME}/.local" }
Please refer to the CMake Documentation about presets for further details.
-
For running the tests, type
cmake --workflow --preset user-test
.
How to use
With the LoadStaticSharedTargets
module installed, 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.9.0
SYSTEM
FIND_PACKAGE_ARGS 1.9.0 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"
)
The find_package()
command 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.