In CMake, assuming one is just setting one property, is there any difference between
set_target_properties(target PROPERTIES prop value)
and
set_property(TARGET target PROPERTY prop value)
?
Cf.
https://cmake.org/cmake/help/v3.0/command/set_property.html https://cmake.org/cmake/help/v3.0/command/set_target_properties.html
which imply there is no difference but aren't that clear.
set_target_properties(target1 target2 ... PROPERTIES prop1 value1 prop2 value2 ...) Sets properties on targets. The syntax for the command is to list all the targets you want to change, and then provide the values you want to set next.
Compiler flags, definitions, source files, include folders, link libraries, and linker options are properties of a target. Avoid using variables to express dependencies between targets: use the visibility levels PRIVATE , INTERFACE , PUBLIC and let CMake figure out the details.
Properties are predefined slots that together describe all aspects of the environment, the state and the project being configured. Remember that the point of running CMake is to have the selected generator output a set of makefiles (or IDE project files).
Consider set_target_properties()
as a specialized form of set_property()
.
set_target_properties(...)
is a convenience function because it allows to set multiple properties of multiple targets.
For example:
add_executable(a ...) add_executable(b ...) set_target_properties( a b PROPERTIES LINKER_LANGUAGE CXX FOLDER "Executable" )
set_property(TARGET ...)
can APPEND
to a list- or APPEND_STRING
to a string-based property of targets.
For example:
add_executable(a ...) set_property( TARGET a APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" )
Note that you also have respective set_*_properties
functions for some of the other types of properties: set_source_files_properties
, set_directory_properties
and set_tests_properties
. Notably absent are setters for install and global properties.
The reason for that is that these functions predate the general set_property
call, which was only introduced with CMake 2.6, together with a general overhaul of the property system to what it is today.
These days, people tend to prefer the generic set_property
, as it is the more modern function and provides a few additional features. It also offers a more consistent syntax than the old functions (eg. set_directory_properties
not allowing to specify the directory as a parameter, set_source_files vs set_directory, etc.).
There is not a strong technical reason for preferring set_property
, but I would consider it slightly better style than using the old, specific functions.
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