【问题标题】:CMake - How to get the SECOND LAST in the directory name?CMake - 如何在目录名称中获得第二个最后?
【发布时间】:2015-12-15 17:59:43
【问题描述】:

所以我有:

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

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

无论如何我可以让 a_second_last_dir 成为可能?返回完整目录的第二低级?

【问题讨论】:

  • 通过附加..?例如。 get_filename_component(a_second_last_dir "/some/dir/sub/../.." ABSOLUTE)
  • 我没有可添加的完整目录.....
  • 你能举一个目录名的例子和你想提取的部分吗?我们是在谈论here 之类的东西吗? CMake 的string() 命令还允许做很多事情。
  • 设法使它工作。我使用目录的目录两次,以便我可以获得父名称:D 不是一个漂亮的解决方案,但它可以工作:P
  • 我仍然认为你可以使用../..。请看下面我的回答。

标签: cmake


【解决方案1】:

把我的 cmets 变成答案

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

我了解您当前的解决方案如下所示:

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 作为输出。

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

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 是前缀),但我认为这里没关系。

如果您希望它是正确的绝对路径,您应该自己为正确的基本目录添加前缀(或参见 CMake 3.4,其中将BASE_DIR 选项引入get_filename_component(... ABSOLUTE))。

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2019-06-23
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    相关资源
    最近更新 更多