Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

cmake execute_process() always fails with "No such file or directory" when I call git

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?

question from:https://stackoverflow.com/questions/65854949/cmake-execute-process-sed-no-such-file-or-directory-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...