gpt4 book ai didi

windows - CMake:如何在参数中用双引号调用 execute_process? a.k.a. 使用 CMake 中的查找来计算匹配字符串的行数

转载 作者:可可西里 更新时间:2023-11-01 13:27:34 27 4
gpt4 key购买 nike

我想调用命令 find来自 execute_process .

find 命令的格式是:

find [/v] [/c] [/n] [/i] [/off[line]] "<String>" [[<Drive>:][<Path>]<FileName>[...]]

因此,字符串必须用双引号引起来。但是,如果在 cmake 中我这样做:

execute_process(COMMAND <firstCommandPipingSomethingToFind> 
COMMAND find "<myString>" /c
OUTPUT_VARIABLE MY_COUNT
OUTPUT_STRIP_TRAILING_WHITESPACE)

我得到一个错误FIND: Parameter format not correct
如果我用 \ 转义双引号,我会得到一个错误 Access denied -\
同样使用双转义 \\ 或双引号 "" 也无济于事。

所以我的问题是:
在调用 execute_process 时是否有办法转义双引号,以便 find 正确接收其参数?

我可以使用 findstr , 不需要双引号;但是它不提供计数功能。我可以通过调用 find /v /c "" 来使用 find , 但同样,我需要双引号!我想在 cmake 之外计算输出行数,并将结果直接存储在 MY_COUNT 变量中。

注意:我正在使用 CMake 3.4.1


要重现问题,您可以使用以下代码,其中 cmake -E echo 用于将字符串提供给 findstrfind:

#This works
execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND findstr ell OUTPUT_VARIABLE DUMMY)
message (STATUS "0) DUMMY=${DUMMY}")

#All of the following don't work
set(MyCommand "find \"ell\" /c")
execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND "${MyCommand}" OUTPUT_VARIABLE DUMMY)
message (STATUS MyCommand=${MyCommand})
message (STATUS "1) DUMMY=${DUMMY}")

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND ${MyCommand} OUTPUT_VARIABLE DUMMY)
message (STATUS "2) DUMMY=${DUMMY}")

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND MyCommand OUTPUT_VARIABLE DUMMY)
message (STATUS "3) DUMMY=${DUMMY}")

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND "find \"ell\" /c" OUTPUT_VARIABLE DUMMY)
message (STATUS "4) DUMMY=${DUMMY}")

哪些输出:

0) DUMMY=hello

MyCommand=find "ell" /c
1) DUMMY=
2) DUMMY=
3) DUMMY=
4) DUMMY=

我也试过

set(MyArgument "\"ell\" /c")
message (STATUS MyArgument=${MyArgument})
execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND find "${MyArgument}" OUTPUT_VARIABLE DUMMY)
message (STATUS "1) DUMMY=${DUMMY}")

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND find ${MyArgument} OUTPUT_VARIABLE DUMMY)
message (STATUS "2) DUMMY=${DUMMY}")

set(MyArgument "\"ell\"")
message (STATUS MyArgument=${MyArgument})
execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND find "${MyArgument}" /c OUTPUT_VARIABLE DUMMY)
message (STATUS "1) DUMMY=${DUMMY}")

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND find ${MyArgument} /c OUTPUT_VARIABLE DUMMY)
message (STATUS "2) DUMMY=${DUMMY}")

哪些输出:

MyArgument="ell" /c
File not found - \ELL\

1) DUMMY=
File not found - \ELL\

2) DUMMY=
MyArgument="ell"
Access denied - \

1) DUMMY=
Access denied - \

2) DUMMY=
CMake Error at CMakeRules.cmake:404 (execute_process):
execute_process given COMMAND argument with no value.
Call Stack (most recent call first):
CMakeLists.txt:24 (include)

如果删除 /c 选项,问题仍然存在。


这个:

execute_process(COMMAND ${CMAKE_COMMAND} -E echo hello COMMAND find \"ell\" OUTPUT_VARIABLE DUMMY)
message (STATUS "DUMMY=${DUMMY}")

输出这个:

Access denied - \

DUMMY=

按照 Tsyvarev 的建议检索哪个字符串被有效地传递给命令行,这个:

execute_process(COMMAND cmd /c echo \"ell\")
execute_process(COMMAND cmd /c echo "ell")
execute_process(COMMAND cmd /c echo ""ell"")

输出这个:

\"ell\"

ell

"" ell\"\"

第三条命令的警告:

Argument not separated from preceding token by whitespace.
This warning is for project developers. Use -Wno-dev to suppress it.

最佳答案

According to CMake developers ,
Windows 的 find 不能通过 cmake 从 execute_process直接调用,因为 CreateProcess API 和非标准方式的限制 find 解析参数。

虽然可以通过创建一个临时批处理脚本来间接调用它。

我通过以下方式解决了计算 Windows 程序输出中字符串出现次数的问题:

file(WRITE ${CMAKE_BINARY_DIR}/countLines.bat [[@find /v /c "" %1]])
execute_process(COMMAND <firstCommandPipingSomethingToFind> COMMAND findstr <regular expression> COMMAND countLines.bat WORKING_DIRECTORY ${CMAKE_BINARY_DIR} OUTPUT_VARIABLE N_MATCHES OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "N_MATCHES=${N_MATCHES}")
file(REMOVE ${CMAKE_BINARY_DIR}/countLines.bat)

介于两者之间的findstr的优点是它支持正则表达式。 find 仅用于 count matching lines .

find 前面的@ 是为了防止脚本中的命令调用在输出变量中结束。

关于windows - CMake:如何在参数中用双引号调用 execute_process? a.k.a. 使用 CMake 中的查找来计算匹配字符串的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34905194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com