CMake:如何规范路径?

编程入门 行业动态 更新时间:2024-10-25 23:22:02
本文介绍了CMake:如何规范路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在CMake中是否有一种可靠的方法来规范路径?

Is there a robust way to normalize paths in CMake?

示例:

# Let's assume that an environment variable MY_ROOT_DIR is set # that points to some directory. set(MYFILE "$ENV{MY_ROOT_DIR}/somefile.txt") message(${MYFILE}) # This will result for example in # Win: C:\path\to\my\root\dir/somefile.txt # Unix based: /path/to/my/root/dir/somefile.txt

在此示例中,将需要规范化 MY_ROOT_DIR (即替换反斜杠)将其用作路径组件之前)。您将如何在CMake中做到这一点?

In this example, it would be required to normalize MY_ROOT_DIR (that is to replace backslashes with slashes) prior to using it as path component. How would you do this in CMake?

CMake(或工具链下方的工具)可能使用混合分隔符( / 或 \ ),也可以不这样做。 CMake使用 / 作为标准分隔符。 CMake针对路径分隔符 \ 生成的典型警告可能类似于以下内容:

CMake (or the tools further down the toolchain) may handle paths with mixed separators (/ or \), or may not. CMake uses / as the standard separator. A typical warning generated by CMake for paths with the wrong path separator \ may look similar to this:

CMake Warning (dev) at cmake_install.cmake:5 (set): Syntax error in cmake code at C:/path/to/my/root/build/cmake_install.cmake:5 when parsing string C:\path\to\my\root/somefile.txt Invalid escape sequence \p Policy CMP0010 is not set: Bad variable reference syntax is an error. Run "cmake --help-policy CMP0010" for policy details. Use the cmake_policy command to set the policy and suppress this warning.

感谢对此的任何提示!

推荐答案

您可以使用 file(TO_CMAKE_PATH) 命令。

You can use the file(TO_CMAKE_PATH) command for this.

TO_CMAKE_PATH 模式将本机< path> 转换为带有正斜杠( / )。输入可以是单个路径,也可以是系统搜索路径,例如 $ ENV {PATH} 。搜索路径将转换为以; 字符分隔的cmake样式列表。

The TO_CMAKE_PATH mode converts a native <path> into a cmake-style path with forward-slashes (/). The input can be a single path or a system search path like $ENV{PATH}. A search path will be converted to a cmake-style list separated by ; characters.

这里是一个示例:

file(TO_CMAKE_PATH "$ENV{MY_DIR_VAR}" ENV_MY_DIR_VAR)

更多推荐

CMake:如何规范路径?

本文发布于:2023-11-23 09:04:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   CMake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!