set_directory_properties can't set subdirectory property

The CMake official documentation says :

set_directory_properties

Set properties of the current directory and subdirectories.

set_directory_properties(PROPERTIES [ ] …)

Sets properties of the current directory and its subdirectories in key-value pairs.

so I try it , in my cmake source dir has a sub dir;

add_subdirectory(sub)
set_directory_properties(  PROPERTIES  Pro1  Value1  )  
 get_directory_property(VAR    DIRECTORY    ./sub    Pro1)

VAR is empty , who can tell me why ?

Because you define the property after calling add_subdirectory() command.
Propagation of properties is done when add_subdirectory() command is executed, so only properties defined at this point are propagated.

Thank you very much for your answer, I changed my code but it doesn’t work:

set_directory_properties(  PROPERTIES  Pro1  Value1  )  
add_subdirectory(sub)
 get_directory_property(VAR    DIRECTORY    ./sub    Pro1)
  

Var is Still Empty! Could you help correct me? Thanks a lot

Try define_property() with the INHERITED argument.

@hsattler you are right, I had forgotten this constraint…

@marc.chevrier @hsattler Thank you very much ,It works !

define_property(DIRECTORY PROPERTY Pro1 INHERITED)

Respect !!!