Hi,

I was just surprised by the scoping rules of CMake when it comes to
function definitions and sub scopes (i.e., subdirectories). I expected
that function definitions adhere to the same scoping rules as variables.
More concretely, I assumed that function definitions in a sub scope are
only valid in the same (or deeper) scope(s) but do not influence
definitions in potential parent scopes.

However, as it turns out, (at least CMake 3.5.1) this is not the case.
CMake seems to use the last definition of a function it encounters,
independent of current scope.

I uploaded a quick minimal working example which shows what I mean under
[1]. I just discovered this behaviour by hunting down a bug in our build
system which was caused by the fact that a function definition in a sub
project overwrote an identically named function in the parent project.

Now I am wondering if this behaviour is actually intended or if it is a
corner case which just never surfaced before. The documentation on
functions [2] unfortunately also omits how this is supposed to behave.

Any pointers would be appreciated.

Regards,
Mario

[1] https://github.com/niosHD/mwe-cmake-function-scopes
[2] https://cmake.org/cmake/help/v3.7/command/function.html

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

Reply via email to