CMmake全局范围内的全局变量 [英] Cmake global variable in macro scope

查看:106
本文介绍了CMmake全局范围内的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建全局列表,并且希望将其附加到宏中. 这是我的设置:

I'm trying to create a global list and I want it appended in a macro. Here is my setup:

project
\__. CMakeLists.txt
\__. level1
    \__. CMakeLists.txt
    \__. level2a
        \__. CMakeLists.txt
    \__. level2b
        \__. CMakeLists.txt

这是我的顶级CMakeLists.txt:

Here is my top level CMakeLists.txt :

cmake_minimum_required(VERSION 2.8)

macro(listappend var)
    list(APPEND MY_GLOBAL_LIST "${var}")
    message(STATUS "LIST IN MACRO SCOPE: ${MY_GLOBAL_LIST}")
endmacro(listappend)

set(MY_GLOBAL_LIST "")

add_subdirectory(level1)
message(STATUS "LIST: ${MY_GLOBAL_LIST}")

# call something here with the global list

level1 CMakeLists.txt只需执行两个add_subdirectory().

level1 CMakeLists.txt simply do two add_subdirectory().

level2 CMakeLists.txt如下:

level2 CMakeLists.txt is as follows:

listappend("test2a")

最后,这是我的输出:

[lz@mac 17:15:14] ~/tmp/cmake/build$ cmake ..
-- LIST IN MACRO SCOPE: test2a
-- LIST IN MACRO SCOPE: test2b
-- LIST: 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lz/tmp/cmake/build

我正在寻找一种在全局范围内附加全局列表的方法,而不必将全局列表变量作为宏的参数. 我不确定是否可以.

I'm looking for a way to have a Global list appended inside the scope of the macro, without having to give the global list variable as parameter of the macro. I'm not sure if it's possible.

我也尝试了CACHE INTERNAL标志,但没有帮助.我真的不怎么处理.

I also tried CACHE INTERNAL flags but it didn't help. I don't really how to handle this.

感谢您的帮助:)

推荐答案

CMake GLOBAL属性是实现在不同级别进行修改的全局列表的一种好方法:

CMake GLOBAL property is a nice way for implement global list which is modified at different levels:

# Describe property
define_property(GLOBAL PROPERTY MY_GLOBAL_LIST
    BRIEF_DOCS "Global list of elements"
    FULL_DOCS "Global list of elements")
# Initialize property
set_property(GLOBAL PROPERTY MY_GLOBAL_LIST "")

# Macro for add values into the list
macro(listappend var)
    set_property(GLOBAL APPEND PROPERTY MY_GLOBAL_LIST "${var}")
endmacro(listappend)

# Do something
add_subdirectory(level1)

# Read list content
get_property(my_list_content GLOBAL PROPERTY MY_GLOBAL_LIST)
message(STATUS "LIST: ${my_list_content}")

这篇关于CMmake全局范围内的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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