I have some target - say it's a library - in a CMakeLists.txt file of a repository I'm working on. I want to the following three to be separate:
CMakeLists.txt; say it needs to be foo.mypackage, and I want the target name to be, say bar, so together it will be used as mypackage::bar by the importer.libbaz, not libfoo nor libbar.How can I achieve this?
I may be able to achieve two of these three using the ALIAS modifier of the add_library() command, but I want all three.
There are target properties that control this: OUTPUT_NAME and EXPORT_NAME. Here's how I would implement your scenario:
cmake_minimum_required(VERSION 3.22)
project(mypackage)
add_library(foo ...)
add_library(mypackage::bar ALIAS foo)
set_target_properties(
foo
PROPERTIES
OUTPUT_NAME "baz"
EXPORT_NAME "bar"
)
include(GNUInstallDirs)
set(mypackage_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/cmake/mypackage"
CACHE STRING "Installation destination for CMake files")
install(TARGETS foo EXPORT mypackage-targets)
install(
EXPORT mypackage-targets
DESTINATION "${mypackage_INSTALL_CMAKEDIR}"
NAMESPACE mypackage::
)
# not shown: other install rules, components (incl. above), etc.
See the docs:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With