CMake-如何在目录名称中获得第二个LAST? [英] CMake - How to get the SECOND LAST in the directory name?

查看:97
本文介绍了CMake-如何在目录名称中获得第二个LAST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有:

get_filename_component(a_dir ${some_file} PATH)
get_filename_component(a_last_dir ${a_dir} NAME)

其中a_last_dir应该返回我目录的最低级别,而a_dir应该返回我的完整目录。

In which a_last_dir should return the lowest level of my directory and a_dir should return my full directory.

无论如何,我可以使a_second_last_dir成为可能吗?要返回完整目录的第二低级别?

Anyway I can make a_second_last_dir possible?? To return the second-lower level of the full directory?

推荐答案

将我的评论变成答案

您可以使用 get_filename_component() 通过附加 ../ ..

我理解您当前的解决方案如下:

I understand your current solution to look like this:

cmake_minimum_required(VERSION 2.8)

project(SecondLastDirName)

set(some_file "some/dir/sub/some_file.h")

get_filename_component(a_dir "${some_file}" PATH)
get_filename_component(a_last_dir "${a_dir}" NAME)

get_filename_component(a_second_dir "${a_dir}" PATH)
get_filename_component(a_second_last_dir "${a_second_dir}" NAME)

message("a_second_last_dir = ${a_second_last_dir}")

哪个给出了 a_second_last_dir = dir 作为输出。

您可以通过以下方式获得相同的输出:

You can get the same output with:

get_filename_component(a_second_dir "${some_file}/../.." ABSOLUTE)
get_filename_component(a_second_last_dir "${a_second_dir}" NAME)

message("a_second_last_dir = ${a_second_last_dir}")

中间的 a_second_dir 路径可能是无效/不存在的路径(因为 CMAKE_CURRENT_SOURCE_DIR 带前缀),但我认为这没关系。

The intermediate a_second_dir path could be an invalid/non-existing path (since CMAKE_CURRENT_SOURCE_DIR is prefixed), but I think it does not matter here.

如果您希望它是正确的绝对路径,则应在正确的基数前面加上前缀dir自己(或参阅CMake 3.4,它在 get_filename_component(... ABSOLUTE)中引入了 BASE_DIR 选项)。

If you want it to be a correct absolute path, you should prefix the correct base dir yourself (or see CMake 3.4 which introduced BASE_DIR option to get_filename_component(... ABSOLUTE)).

这篇关于CMake-如何在目录名称中获得第二个LAST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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