Variables set with PARENT_SCOPE are empty in the corresponding child scope. Why?

后端 未结 4 2115
轮回少年
轮回少年 2021-02-12 16:17

Consider the following minimal example:

.
├── bar
│   └── CMakeLists.txt
└── CMakeLists.txt

where ./CMakeLists.txt is



        
4条回答
  •  感动是毒
    2021-02-12 17:09

    Peter explained well the reason for this behaviour.

    A workaround I usually use in this case is to set a cached variable, which will be visible everywhere:

    set(BAR "Visible everywhere"
            CACHE INTERNAL ""
    )
    

    INTERNAL is to make it not visible from cmake-gui. INTERNAL implies FORCE, making sure it gets updated if you change something for example in your folder structure. The empty string is a description string, that you might want to fill if you believe it's necessary.

    Note, though, that the correct approach is attaching properties to targets whenever possible, like using target_incude_directories, and propagate them to other targets by setting dependencies.

提交回复
热议问题