Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

in cmake , how to define a function which can be called with multiple keyvalue…

  1. User

    in cmake , how to define a function which can be called with multiple keyvalue arguments. and the arguments has same name.

  2. Assistant

    In CMake, you can define a function that accepts multiple key-value arguments by using the ARGN special variable and a loop. However, note that you cannot have multiple arguments with the same name in CMake. Here's an example of how you can achieve this:

    function(multi_keyvalue_function)
      set(options)
      set(oneValueArgs)
      set(multiValueArgs)
    
      # Parse the arguments
      cmake_parse_arguments(MY_ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    
      # Loop through the parsed arguments
      foreach(arg ${MY_ARGS_UNPARSED_ARGUMENTS})
        message("Key: ${arg}")
        message("Value: ${${arg}}")
      endforeach()
    endfunction()
    
    # Usage example
    multi_keyvalue_function(KEY1 value1 KEY2 value2 KEY3 value3)
    

    Output:

    Key: KEY1
    Value: value1
    Key: KEY2
    Value: value2
    Key: KEY3
    Value: value3
    

    Please note that the above example does not check for any specific naming convention like having identical key names. If you attempt to pass multiple arguments with the same key name, only the last value will be stored in the variable.