CMake: Is there a difference between set_property (TARGET ...) and set_target_properties?

In CMake, assuming that one only sets one property, is there a 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 means there is no difference, but that is not clear.

+5
source share
3 answers

Consider set_target_properties() as a specialized form of set_property() .

Benefits...

  • set_target_properties(...) is a convenience function, because it allows you to set several properties of several goals.

    For instance:

     add_executable(a ...) add_executable(b ...) set_target_properties( ab PROPERTIES LINKER_LANGUAGE CXX FOLDER "Executable" ) 
  • set_property(TARGET ...) can APPEND pass to a list or APPEND_STRING property based on object strings.

    For instance:

     add_executable(a ...) set_property( TARGET a APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" ) 

References

+4
source

The difference is that with set_property you can define the scope. In fact, you have more options with set_property , except to just specify the target, for example, specify the source files in the list in order to have a specific property.

For instance:

 set_property(SOURCE src1.cpp src2.cpp PROPERTY SKIP_AUTOMOC ...) 

This will add the SKIP_AUTOMOC property to the listed source files. (This is for Qt, where Moc'ing of objects happens automatically, and sometimes you don’t want it).

Contrast with set_target_properties , where you must specify Target and the property and its value.

 set_target_properties(target PROPERTIES CXX_STANDARD 11 ...) 

Hope this helps!

+5
source

Note that you also have the corresponding set_*_properties functions for some other property types: set_source_files_properties , set_directory_properties and set_tests_properties . In particular, there are no setters for installation and global properties.

The reason for this is that these functions precede the general set_property call, which was introduced only with CMake 2.6, together with the general overhaul of the property system to this day.

These days, people prefer the generic set_property , as it is a more modern function and provides several additional functions. It also offers a more consistent syntax than older functions (for example, set_directory_properties does not allow you to specify a directory as a parameter, set_source_file s vs set_director y , etc.) ..

There is no strong technical reason to prefer set_property , but I would think a little better than using the old concrete functions.

+4
source

Source: https://habr.com/ru/post/1273549/


All Articles