Consider the following minimal example:
.
├── bar
│ └── CMakeLists.txt
└── CMakeLists.txt
where ./CMakeLists.txt
is
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.