cmake usefulness of aliases

2019-01-27 21:59发布

I dont quite get the application of alias expressions. I understand that I can write something like this

cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
project(myLibs)
add_library(${PROJECT_NAME} src/test.cpp)
add_library(myLibs::myLibs ALIAS ${PROJECT_NAME})
...

and then use

  target_link_libraries(${TARGET_NAME}
  myLibs::myLibs

in another file to link the library to some executable, etc.

but why would I do that? I might as well skip the alias definition and just use the targetname of the built library directly

target_link_libraries(${TARGET_NAME}
myLibs
  • Can anyone explain to me why aliases exist
  • why do they employ the :: syntax? is the alias not completely arbitrary?

标签: cmake alias
1条回答
ゆ 、 Hurt°
2楼-- · 2019-01-27 22:22

Taking your add_library() example, the CMake target's name would e.g. directly be linked to the target's output file names.

So ALIAS targets are mainly used to give the target a more spelling or structured name by e.g. adding a "namespace".

The cmake-developer documentation gives the following advice on namespaces:

When providing imported targets, these should be namespaced (hence the Foo:: prefix); CMake will recognize that values passed to target_link_libraries() that contain :: in their name are supposed to be imported targets (rather than just library names), and will produce appropriate diagnostic messages if that target does not exist (see policy CMP0028).

查看更多
登录 后发表回答