在Linux机器上,从cmake项目,我正在尝试使用execute_process调用git,以便我可以将源代码控制中的信息包含到我的应用程序中。
我创建了一个小测试来尝试打印git版本:
cmake_minimum_required (VERSION 2.8)
set (git_cmd "/usr/bin/git --version")
#set (git_cmd "ls") # returns success if you uncomment this line
message(STATUS "git cmd: ${git_cmd}")
execute_process(COMMAND ${git_cmd}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_ver)
message(STATUS "git ver[${git_result}]: ${git_ver}")
configure_file (
"${PROJECT_SOURCE_DIR}/versionInfo.h.in"
"${PROJECT_BINARY_DIR}/versionInfo.h"
)
运行make时,它会提供以下输出:
-- git cmd: /usr/bin/git --version
-- git ver[No such file or directory]:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rsanderson/build/githash:
但如果我将命令更改为ls
,结果有效,我会看到dir列表打印。我还检查了哪个git确实在/ usr / bin。
我在这里缺少什么想法?
答案 0 :(得分:48)
您必须将参数作为第二个选项传递,如下所示:
cmake_minimum_required (VERSION 2.8)
set (git_cmd "git")
set (git_arg "--version")
message(STATUS "git cmd: ${git_cmd}")
execute_process(COMMAND ${git_cmd} ${git_arg}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE git_result
OUTPUT_VARIABLE git_ver)
message(STATUS "git ver[${git_result}]: ${git_ver}")