所以我有:
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() https://cmake.org/cmake/help/v3.4/command/get_filename_component.html通过附加../..
.
我了解您当前的解决方案如下所示:
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)
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)