2015-12-21 53 views
2

有没有一种方法可以使用存储在变量中的名称(用于将函数传递给函数等)在CMake中调用函数?如何在CMake中调用其名称存储在变量中的函数

这是我曾尝试:

cmake_minimum_required(VERSION 3.0) 

function(doThing) 
endfunction() 

set(FuncVar doThing) 

${FuncVar}() 

哪些失败,此错误:

Parse error. Expected a command name, got unquoted argument with text "${FuncVar}". 
-- Configuring incomplete, errors occurred! 

我不明白为什么这不应该工作,但我还是那句话是新来的CMake,所以我知道什么。

谢谢你的帮助!

+0

此语法不支持。你究竟在努力实现什么?我从来没有需要这样的技术。 – SirDarius

+0

我想创建一个系统,其中的基础系统配置派生。我想变量是要走的路。谢谢! –

回答

6

我已经使用文件解决了此问题。

比方说你有:

function(do what) 
    ... 
endfunction() 

要拨打不同专业根据“是什么”。然后,你可以这样做:

function(do what) 
    include("do-${what}.cmake") 
    do_dynamic() 
endfunction() 

而且在文件中做-something.cmake:

function(do_dynamic) 
    ... 
endfunction() 

,只要你想,你可以创建许多专业化的文件...

1

您好我已经写eval为cmake(它是一样快,我可以做到) herehere是代码,因为它是我的cmakepp库的一部分。

我写的eval两个版本(evaleval_ref,因为先不给你访问到PARENT_SCOPE而后者则)

但是这一点,如果您使用cmakepp只会帮助,并因为这可能是一个对我来说,我修改它与香草cmake工作:

## evals the specified cmake code. 
## WARNING: there is no way to set(<var> <value> PARENT_SCOPE) 
## because of the extra function scope defined by eval. 
## WARNING: allowing eval can of course be dangerous. 
function(eval __eval_code) 

    # one file per execution of cmake (if this file were in memory it would probably be faster...) 
    # this is where the temporary eval file will be stored. it will only be used once per eval 
    # and since cmake is not multihreaded no race conditions should occure. however if you start 
    # two cmake processes in the same project this could lead to collisions 
    set(__eval_temp_file "${CMAKE_CURRENT_BINARY_DIR}/__eval_temp.cmake") 


    # write the content of temp file and include it directly, this overwrite the 
    # eval function you are currently defining (initializer function pattern) 
    file(WRITE "${__eval_temp_file}" " 
function(eval __eval_code) 
    file(WRITE ${__eval_temp_file} \"\${__eval_code}\") 
    include(${__eval_temp_file}) 
endfunction() 
    ") 

include("${__eval_temp_file}") 
## now eval is defined as what was just written into __eval_temp_file 


## since we are still in first definition we just need to execute eval now 
## (which calls the second definition of eval). 
eval("${__eval_code}") 


endfunction() 
相关问题