cmake_parse_arguments 存储空字符串

cmake_parse_arguments storing empty strings

我正在尝试使用 cmake_parse_arguments() 的函数签名变体,镜像宏的示例:

include(CMakeParseArguments)

set(prefix PREFIX_)
set(${prefix}VAR "foo")
message(DEBUG " value of " "${prefix}VAR" " is " ${${prefix}VAR})

function(func)
    set(prefix ARG_)
    set(options OPTION)
    set(oneValueArgs VALUE)
    set(multiValueArgs MULTIVALUE)
    cmake_parse_arguments(PARSE_ARGV 0 "${prefix}" "${options}" "${oneValueArgs}" "${multiValueArgs}")

    message(DEBUG ${${prefix}UNPARSED_ARGUMENTS})
    message(DEBUG ${${prefix}OPTION})
    message(DEBUG ${${prefix}VALUE})
    message(DEBUG ${${prefix}MULTIVALUE})
endfunction(func)

func(VALUE 42 MULTIVALUE "foo" "bar" "baz")

消息输出为:

DEBUG value of PREFIX_VAR is foo
DEBUG
DEBUG
DEBUG
DEBUG

输出只有空字符串。怎么回事?

来自 cmake_parse_arguments 的文档:

cmake_parse_arguments will consider for each of the keywords listed in <options>, <one_value_keywords> and <multi_value_keywords> a variable composed of the given <prefix> followed by "_" and the name of the respective keyword.

例如,运行此方法后填充的变量之一是<prefix>_UNPARSED_ARGUMENTS。因为您的前缀 already 包含下划线,所以它会扩展为 ARG__UNPARSED_ARGUMENTS(在 ARG 之后有两个下划线)。因此,将额外的下划线添加到您的 message() 调用会产生解析值:

include(CMakeParseArguments)

set(prefix PREFIX_)
set(${prefix}VAR "foo")
message(DEBUG " value of " "${prefix}VAR" " is " ${${prefix}VAR})

function(func)
    set(prefix ARG_)
    set(options OPTION)
    set(oneValueArgs VALUE)
    set(multiValueArgs MULTIVALUE)
    cmake_parse_arguments(PARSE_ARGV 0 "${prefix}" "${options}" "${oneValueArgs}" "${multiValueArgs}")

    # Add additional underscore to the variables expanded here!
    message(DEBUG ${${prefix}_UNPARSED_ARGUMENTS})
    message(DEBUG ${${prefix}_OPTION})
    message(DEBUG ${${prefix}_VALUE})
    message(DEBUG ${${prefix}_MULTIVALUE})
endfunction(func)

func(VALUE 42 MULTIVALUE "foo" "bar" "baz")

这将打印以下内容:

DEBUG value of PREFIX_VAR is foo
DEBUG
DEBUG FALSE
DEBUG 42
DEBUG foobarbaz