CMake报价转义难题 [英] CMake quote escape conumdrum

查看:77
本文介绍了CMake报价转义难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法绕过CMake的逃生规则。给出:

I just don't seem to be able to wrap my head around CMake's escape rules. Given:

set(X A B C)
add_custom_target( works COMMAND DUMMY=0 X="${X}" env | grep ^X= COMMENT "This works")
add_custom_target( fails COMMAND X="${X}" env | grep ^X= COMMENT "This fails")

目的是执行命令 X = ABC env 。自定义目标可以正常工作正确构造命令,而失败会错误地执行以下命令:

The intention is to execute command X="A B C" env. The custom target works correctly constructs the command, where as fails incorrectly executes:

X=\"A B C\" env ...

但是为什么?

推荐答案

实际上您遇到了两个问题:

Actually you ran into two problems:


  1. 不要在自定义命令中引用CMake变量。 CMake将为您执行必要的转义序列。

  2. COMMAND 之后的第一个文字被假定为命令名称或文件。因此CMake尝试将其作为单个字符串来处理。

  1. Don't quote CMake variables in custom commands. CMake will do the necessary escape sequences for you.
  2. The first literal after COMMAND is assumed to be a command name or file. So CMake tries to handle it as a single "string".

因此我更改了引号和 env 调用,以下内容确实对我有用:

So I changed the quoting and the env call and the following did work for me:

cmake_minimum_required(VERSION 2.8)

project(QuotedStrings)

set(X "A B C")
add_custom_target( works COMMAND env DUMMY=0 X=${X} | grep ^X= COMMENT "This works")
add_custom_target( fails_no_more COMMAND env X=${X} | grep ^X= COMMENT "This works too")

有关更多详细信息和可能性,请参见:

For more details and possibilities see:

  • cmake: How to include literal double-quote in custom command?
  • cmake: when to quote variables?

这篇关于CMake报价转义难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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