How to define a cmake macro in a sub_directory that uses the CURRENT_SOURCE_DIR?

后端 未结 2 889
北荒
北荒 2021-01-25 00:25

What I want to do is to create a CMakeLists.txt that defines a convenience macro to use in parent scope. I can use the macro just fine. However, I used the ${

2条回答
  •  轮回少年
    2021-01-25 01:07

    You have to transfer CMAKE_CURRENT_LIST_DIR outside the macro into another variable or - in your case - a user defined global property:

    set_property(GLOBAL PROPERTY DoStuffPath "${CMAKE_CURRENT_LIST_DIR}")
    
    macro(do_stuff)
        get_property(_my_marcros_file GLOBAL PROPERTY DoStuffPath)
        message("This CMakeLists.txt file is in ${_my_marcros_file}")
    endmacro()
    

    That also works when the macros are defined in a file added with include().

    References

    • In CMake, how can I find the directory of an included file?
    • What's the CMake syntax to set and use variables?

提交回复
热议问题