Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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.

like image 974
Mohan Avatar asked Nov 21 '17 14:11

Mohan


People also ask

What is Set_target_properties?

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.

What are target properties in CMake?

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.

What is Property in CMake?

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).


2 Answers

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

Advantages of ...

  • 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}" )   

References

  • How to change the name of the output binary to not be a.out with CMake?
  • target_include_directories prior to 2.8.12?
  • Appending compiler flags to a file with CMake
like image 109
Florian Avatar answered Sep 27 '22 21:09

Florian


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.

like image 24
ComicSansMS Avatar answered Sep 27 '22 22:09

ComicSansMS