Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have separate names for a target, its filename, and its export name?

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:

  1. The target name I use in CMakeLists.txt; say it needs to be foo.
  2. The exported target name, which others will use after importing my installed repository: The prefix is mypackage, and I want the target name to be, say bar, so together it will be used as mypackage::bar by the importer.
  3. The library file basename; I want it to be 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.

like image 712
einpoklum Avatar asked Oct 28 '25 13:10

einpoklum


1 Answers

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:

  • https://cmake.org/cmake/help/latest/prop_tgt/OUTPUT_NAME.html
  • https://cmake.org/cmake/help/latest/prop_tgt/EXPORT_NAME.html
like image 177
Alex Reinking Avatar answered Oct 31 '25 10:10

Alex Reinking