Function vs. Macro in CMake

前端 未结 5 2074
执念已碎
执念已碎 2020-12-12 14:45

The official document of CMake 2.8.12 says about macro

When it is invoked, the commands recorded in the macro are first modified by rep

5条回答
  •  没有蜡笔的小新
    2020-12-12 15:12

    Another notable difference between function() and macro() is the behavior of return().

    From the cmake documentation of return():

    Note that a macro, unlike a function, is expanded in place and therefore cannot handle return().

    So because it is expanded in place, in a macro() it returns from the caller. While in a function it just exits the function()

    Example:

    macro(my_macro)
        return()
    endmacro()
    
    function(my_function)
        return()
    endfunction()
    
    my_function()
    message(hello) # is printed
    my_macro()
    message(hi) # is not printed
    

提交回复
热议问题