如何在cmake的函数中设置全局变量? [英] How to set the global variable in a function for cmake?

查看:70
本文介绍了如何在cmake的函数中设置全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 CMakeLists.txt 来生成文件并编译生成的文件.我创建了一个函数来将一些文件路径字符串添加到全局列表变量中.

I'm writing a CMakeLists.txt to generate files and compile the generated files. I create a function to add some file path strings to a global list variable.

我的 CMakeLists.txt:

My CMakeLists.txt:

set(source_list "nothing")
function(test file_path)
    list(APPEND source_list ${file_path})
endfunction(test)
test(abc.txt)
test(def.txt)
message("At last, the source_list is:"${source_list}"")

cmake 输出:

At last, the source_list is:"nothing"

有人建议用宏代替函数,但我确实需要使用局部变量,所以我需要用函数代替宏.

Someone suggested that to use macro instead of function, but I do need use local variable, so I need to use the function instead of macro.

如何在函数test()中正确设置全局变量source_list?cmake不能用简单正常的方式来做吗?

How can I correctly set the global variable source_list in the function test()? Can't cmake do it in a simple and normal way?

推荐答案

你需要使用 set 而不是 list 来影响父作用域中的变量.

You need to use set instead of list to affect the variable in the parent scope.

因此将您的 list 命令替换为:

So replace your list command with:

set(source_list ${source_list} ${file_path} PARENT_SCOPE)

这篇关于如何在cmake的函数中设置全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆