我们可以知道宏或函数在cmake中的目录吗 [英] Can we know the directory where the macro or functions are located in cmake

查看:165
本文介绍了我们可以知道宏或函数在cmake中的目录吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CMAKE中,它定义了以下变量来指示文件目录:

In CMAKE, it defines the following variables to indicate the directories of files:

CMAKE_CURRENT_LIST_DIR
CMAKE_CURRENT_BINARY_DIR
CMAKE_CURRENT_SOURCE_DIR

在处理CMake脚本时它们很有用。但是,它们都不能告诉您定义宏或功能的目录。给出以下示例CMakeLists.txt来说明我的问题

They are useful when you process CMake scripts. However, none of them can tell you the directory where MACROs or functions are defined. Give the following example CMakeLists.txt to illustrate my question

project(Hello)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/my_macro.cmake)
test_macro()

然后为my_macro.cmake,我们有 test_macro()的定义:

Then for the my_macro.cmake, we have definitions for test_macro():

macro(test_macro)
  message("hello")
  #?? Can we know the folder of this macro is located?
  #?? the macro definition file's location
endmacro()


推荐答案

我不认为有一个现成的变量,但是您可以轻松创建自己的变量:

I don't think there's an off-the-shelf variable for that, but you could easily make your own:

my_macro.cmake

set(test_macro__internal_dir ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")

macro(test_macro)
  message(STATUS "Defined in: ${test_macro__internal_dir}")
endmacro()

set()行将在包含文件时进行处理,并且该处理中 CMAKE_CURRENT_LIST_DIR 的值缓存以供将来在宏内使用。

The set() line will be processed when the file is included, and the value of CMAKE_CURRENT_LIST_DIR from that processing cached for future use inside the macro.

这篇关于我们可以知道宏或函数在cmake中的目录吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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