如何在使用CURRENT_SOURCE_DIR的子目录中定义cmake宏? [英] How to define a cmake macro in a sub_directory that uses the CURRENT_SOURCE_DIR?

查看:390
本文介绍了如何在使用CURRENT_SOURCE_DIR的子目录中定义cmake宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是创建一个 CMakeLists.txt ,它定义在父范围内使用的便捷宏。我可以使用宏。但是,不幸的是,我使用了 $ {CMAKE_CURRENT_SOURCE_DIR} ,它不是宏定义所在的CMake脚本的目录,而是它的调用源。有什么办法可以改变呢?

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 ${CMAKE_CURRENT_SOURCE_DIR} which is unfortunately not the directory of the CMake script the macro is defined in, but the one it is called from. Is there any way I can change that?

MWE

MWE

cmake_minimum_required(VERSION 3.6)
project(git_info CXX)
macro(do_stuff)
    message("This CMakeLists.txt file is in ${CMAKE_CURRENT_SOURCE_DIR}")
endmacro()

我发现的一种丑陋方法是将变量导出到包含路径和使用情况的父范围在宏中。但是我宁愿只导出宏定义(如果可能的话),以保持整洁。

One ugly way I found was to export variables to the parent scope that contain the path and use that in the macro. But I would prefer to only export the macro definition, if possible, to keep things clean.

编辑:
一点。我的顶层 CMakeLists.txt 有一个文件夹,上面的MWE有一个文件夹( my_folder CMakeLists.txt 。顶级 CMakeLists.txt 如下所示:

To clarify a bit. I have one folder with my top-levelCMakeLists.txt and one folder (my_folder) inside with the above MWE CMakeLists.txt. The top-level CMakeLists.txt looks as follows:

cmake_minimum_required(VERSION 3.6)
project(top_project CXX)
add_subdirectory(my_folder)
do_stuff()


推荐答案

您必须转移 CMAKE_CURRENT_LIST_DIR 移到宏之外的另一个变量中,或者-在您的情况下-用户定义的全局属性:

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()

当在使用 include()

参考

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

这篇关于如何在使用CURRENT_SOURCE_DIR的子目录中定义cmake宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆