通过 CMake 运行 bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25687890/
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
Running a bash command via CMake
提问by assignment_operator
I'm trying to have CMake either run three bash commands or a bash script. However, I can't seem to get it to work.
我试图让 CMake 运行三个 bash 命令或一个 bash 脚本。但是,我似乎无法让它发挥作用。
The bash commands are:
bash 命令是:
cd ${CMAKE_SOURCE_DIR}/dependencies/library
make
cd ${CMAKE_BINARY_DIR}
Essentially, I would like CMake to build the library in that directory if it does not already exist.
本质上,如果库不存在,我希望 CMake 在该目录中构建库。
Here's the CMake code I tried:
这是我尝试过的 CMake 代码:
if(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
execute_process(COMMAND cd ${CMAKE_SOURCE_DIR}/dependencies/library)
execute_process(COMMAND make)
execute_process(COMMAND cd ${CMAKE_BINARY_DIR})
endif(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
However, it's not building anything. What am I doing wrong?
但是,它并没有构建任何东西。我究竟做错了什么?
Also, while I'm here asking this: should the third command, to move to the binary folder, be included?
另外,虽然我在这里问这个问题:是否应该包括第三个命令,移动到二进制文件夹?
Thanks!
谢谢!
回答by Phil Be
execute_process()
is executed during configure time. But you want this to run at build time, thus add_custom_command()
and add_custom_target()
is what you're looking for.
execute_process()
在配置期间执行。但你希望在构建时运行,因此add_custom_command()
和add_custom_target()
是你在找什么。
In this special case you want to generate an output file, so you should go for add_custom_command()
(both are essentially the same, but command
produces one or multiple output files, while target
does not.
在这种特殊情况下,您想要生成一个输出文件,因此您应该选择add_custom_command()
(两者本质上相同,但command
会生成一个或多个输出文件,而target
不会。
The cmake snippet for this should look something like the following:
用于此的 cmake 代码段应如下所示:
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/dependencies/library/lib.o
WORKING_DIR ${CMAKE_SOURCE_DIR}/dependencies/library
COMMAND make
)
You then have to add the output file in another target as dependency, and everything should (hopefully) work as expected.
然后,您必须将输出文件添加到另一个目标中作为依赖项,并且一切都应该(希望)按预期工作。
You can also add DEPENDS
statements to the add_custom_command()
call to rebuild the object file in case some input sources have changed.
如果某些输入源发生更改,您还可以DEPENDS
向add_custom_command()
调用添加语句以重建目标文件。