Using Aliases in CMake

When defining CMake goals, you can create alias targets so that the alias name can be used to refer to a specific object in subsequent commands. For instance,

add_library(foo_lib foo.cpp bar.cpp bat.cpp)
add_library(foo::lib ALIAS foo_lib)

As I understand it, this has the advantage that the name foo_libdoes not appear as the target. However, given this alias name, I would like to set all kinds of properties for them, such as:

set_target_properties(foo::lib PROPERTIES COMPILE_DEFINITIONS ...)
target_include_directories(foo::lib PUBLIC ... PRIVATE ...)

but this is not possible, unfortunately, since CMake Error: set_target_properties cannot be used for the purpose of ALIAS. I don’t understand why this should not be possible, because I would like to determine the name of my lib once and refer to this alias whenever I want to set the target property. Any tips on how to use ALIAS targets “correctly”? What is the purpose of ALIAS goals other than that they don’t appear as Make Objects?

+4
source share
1 answer

ALIAS is like a synonym. The goal of ALIAS is just another name for the original. Thus, the requirement for the ALIAS target cannot be changed - you cannot configure its properties, set it, etc.

- , , (, ):

if(FOO_USE_SHIPPED)
    add_library(FOO ...) # Library FOO shipped with our project
endif()

...

# We need FOO_test for testing
if(FOO_USE_SHIPPED)
    add_library(FOO_test ALIAS FOO) # Use our library
else()
    add_library(FOO_test IMPORTED)
    set_target_property(FOO_test ...) # Use external library
endif()
+3

All Articles