从 CMake 文件执行 bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35072473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing bash commands from a CMake file
提问by user3308321
I am having trouble understanding CMake. What I want to do is execute the following bash command during the build process:
我在理解 CMake 时遇到问题。我想要做的是在构建过程中执行以下 bash 命令:
date +"%F %T" > timestamp
date +"%F %T" > timestamp
This fetches the current date and writes it to a file. However, I cannot seem to reproduce this simple action using CMake commands.
这将获取当前日期并将其写入文件。但是,我似乎无法使用 CMake 命令重现这个简单的操作。
Here are a few things that I've tried:
以下是我尝试过的一些事情:
execute_process(COMMAND "date +'%F %T' > timestamp")
execute_process(COMMAND "date +'%F %T' > timestamp")
add_custom_command(OUTPUT timestamp COMMAND date +"%F %T")
add_custom_command(OUTPUT timestamp COMMAND date +"%F %T")
file(WRITE timestamp date +"%F %T")
file(WRITE timestamp date +"%F %T")
Neither seem to work. I almost wonder if they are even being executed at all.
两者似乎都不起作用。我几乎想知道他们是否正在被处决。
I have a very limited knowledge of how CMake and its syntax, so I am probably doing things very wrong. I am hoping someone could point me in the right direction. Thanks!
我对 CMake 及其语法的了解非常有限,所以我做的事情可能非常错误。我希望有人能指出我正确的方向。谢谢!
回答by user3308321
I think my main issue was the lack of quotes around my command arguments. Also, thanks to @Mark Setchell I realized I should be using OUTPUT_VARIABLE
in lieu of OUTPUT
我认为我的主要问题是我的命令参数周围缺少引号。另外,感谢@Mark Setchell,我意识到我应该使用OUTPUT_VARIABLE
它来代替OUTPUT
At any rate, here is the answer I arrived at:
无论如何,这是我得出的答案:
execute_process (
COMMAND bash -c "date +'%F %T'"
OUTPUT_VARIABLE outVar
)
This stores the output of the bash command into the variable outVar
这将 bash 命令的输出存储到变量中 outVar
file(WRITE "datestamp" "${outVar}")
And this writes the contents of outVar
to a file called "datestamp".
这会将 的内容写入outVar
名为“datestamp”的文件。
回答by Alex Punnen
Note -Using bash -c will also prep-end a new line to end of the variable which will cause make to complain depending on how you are using it
注意 - 使用 bash -c 还会在变量的末尾添加一个新行,这将导致 make 根据您的使用方式抱怨
build.make: *** missing separator. Stop.
build.make: *** 缺少分隔符。停止。
this should solve the above
这应该解决上述问题
execute_process(COMMAND which grpc_cpp_plugin OUTPUT_VARIABLE GRPC_CPP_PLUGIN)
string(STRIP ${GRPC_CPP_PLUGIN} GRPC_CPP_PLUGIN)
message(STATUS "MY_VAR=${GRPC_CPP_PLUGIN}")