2015-12-21 67 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() 
相關問題