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:
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
). - For building the module, type
cmake --preset default
. This will populate thebuild
directory with a CMake build tree. - Now, you can build with
cmake --build ./build
. You should see some informative terminal output. - Finally, install the built artifacts to the
bin
folder withcmake --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 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.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.