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.
What you need:
- Git (optional) for getting the code and contributing to the project
- CMake and Ninja for building the project
Use your distribution's package manager to install the necessary packages:
$ sudo apt-get install cmake ninja-build
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.
$ brew install cmake ninja
$ sudo port -vb install cmake-devel ninja
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.
-
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
cmake --list-presets
. A list of available build configurations will be shown to you. -
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 theCMAKE_INSTALL_PREFIX
variable:$ cmake --preset ninja -D "CMAKE_INSTALL_PREFIX=/path/to/install/prefix"
-
Now, you can build with
cmake --build --preset ninja
. You should see some informative terminal output. -
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 requiresudo
.
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.
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.