cmake execute_process() always fails with “No such

2019-02-07 23:14发布

问题:

On a linux machine, from a cmake project, I'm trying to call git using execute_process so that I can include info from source control into my app.

I created a little test to try and print the git version:

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

Which gives the following output when you run 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: 

But if I change the command to ls the result is valid and I see the dir listing print. I also checked with which that git is indeed in /usr/bin.

Any ideas of what I'm missing here?

回答1:

You have to pass the arguments as a second option like this:

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


标签: cmake