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

后端 未结 4 832
傲寒
傲寒 2021-02-12 16:39

Consider the following minimal example:

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

where ./CMakeLists.txt is



        
4条回答
  •  抹茶落季
    2021-02-12 17:03

    Context: my project consists of several executables and libraries. For a library, e.g. bar, I'd like to set a variable bar_INCLUDE_DIR which is added to the include paths of any depending executable.

    There is a much better way to do this than to set variables in the parent scope. CMake allows a target to specify include directories, preprocessor symbols etc. that depending targets can use. In your case, you can use target_include_directories.

    For example:

    target_include_directories(my_target PUBLIC my_inc_dir)
    

提交回复
热议问题