Optional Argument in cmake macro

Any arguments named in your macro definition are required for callers. However, your macro can still accept optional arguments as long as they aren’t explicitly named in the macro definition.

That is, callers are permitted to pass in extra arguments (which were not named in the macro’s argument list). Your macro can check for the existence of such extra arguments by checking the ${ARGN} variable.

Beware: ARGN is not a “normal” cmake variable. To use it with the list() command, you’ll need to copy it into a normal variable first:

macro (mymacro required_arg1 required_arg2)
    # Cannot use ARGN directly with list() command,
    # so copy it to a variable first.
    set (extra_args ${ARGN})
    
    # Did we get any optional args?
    list(LENGTH extra_args extra_count)
    if (${extra_count} GREATER 0)
        list(GET extra_args 0 optional_arg)
        message ("Got an optional arg: ${optional_arg}")
    endif ()
endmacro (mymacro)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)