Skip to main content
 首页 » 编程设计

cmake之当我调用git时,cmake execute_process()总是失败并带有 “No such file or directory”

2024年11月01日17lexus

在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,则结果有效,并且我看到目录列表已打印。我还检查了/ usr / bin中确实存在哪些git。

关于我在这里缺少什么的任何想法?

请您参考如下方法:

您必须像这样将参数作为第二个选项传递:

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}")