我想在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
用于将字符串提供给findstr
和find
:
#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.
答案 0 :(得分:0)
According to CMake developers,
由于CreateProcess API和非标准方式find
解析参数的限制,cmake无法从execute_process
内直接调用Windows 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
是为了防止脚本中的命令调用以输出变量结束。