Is there a way to call a function in CMake using the name that is stored in a variable (for passing functions to functions, etc)?
Here is what I have tried:
cmake_minimum_required(VERSION 3.0)
function(doThing)
endfunction()
set(FuncVar doThing)
${FuncVar}()
Which fails with this error:
Parse error. Expected a command name, got unquoted argument with text "${FuncVar}".
-- Configuring incomplete, errors occurred!
I can't see why this shouldn't work, but then again I am new to CMake so what do I know.
Thank you for any help!
I have solved this with a workaround using files.
Lets say you have:
You want to call different specializations depending on 'what'. You can then do:
And in file do-something.cmake:
You can create as many specialization files as you want...
Hi I have written
eval
for cmake (and it is as fast as i can make it) here and here is the code as it is part of my cmakepp library.I have written two versions of
eval
(eval
andeval_ref
because the first does not give you access to thePARENT_SCOPE
whereas the latter does)however this will only help if your use cmakepp and as that might be a dealbreaker for you I modified it to work with vanilla cmake:
Without workarounds, CMake itself does not support function pointers or indirect calls. For example someone asked on the CMake mailing list in 2011 about a
call
function to achieve what you wanted to do: https://cmake.org/pipermail/cmake/2011-September/046124.html