通过CMake的运行bash命令 [英] Running a bash command via CMake

查看:1245
本文介绍了通过CMake的运行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有任何的CMake运行三个命令的bash或者bash脚本。不过,我似乎无法得到它的工作。

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.

bash的命令是:

    cd ${CMAKE_SOURCE_DIR}/dependencies/library
    make
    cd ${CMAKE_BINARY_DIR}

从本质上讲,我想CMake的建库在该目录中,如果它不存在。

Essentially, I would like CMake to build the library in that directory if it does not already exist.

这里的CMake的code我尝试:

Here's the CMake code I tried:

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?

谢谢!

推荐答案

<一个href=\"http://www.cmake.org/cmake/help/v3.0/command/execute_process.html\"><$c$c>execute_process()期间配置的时间执行。但你希望在构建时运行,因此<一个href=\"http://www.cmake.org/cmake/help/v3.0/command/add_custom_command.html\"><$c$c>add_custom_command()和<一个href=\"http://www.cmake.org/cmake/help/v3.0/command/add_custom_target.html\"><$c$c>add_custom_target()就是你在寻找什么。

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.

在这种特殊情况下要生成一个输出文件,所以你应该去为 add_custom_command()(两者在本质上是相同的,但命令产生一个或多个输出文件,而目标没有。​​

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.

此CMake的片段应该类似于以下内容:

The cmake snippet for this should look something like the following:

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.

您还可以添加 DEPENDS 语句到 add_custom_command()调用重建目标文件的情况下,一些输入来源已经改变了。

You can also add DEPENDS statements to the add_custom_command() call to rebuild the object file in case some input sources have changed.

这篇关于通过CMake的运行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆