在 CMake 中默认构建自定义目标 [英] Build custom target by default in CMake
问题描述
我想用 cmake 运行一个自定义命令.这听起来像是一个非常简单的任务/问题,但令人沮丧的是找到一个例子是多么困难.
I'd like to run a custom command with cmake. That sounds like an incredibly simple task/question, but it's frustrating how difficult it is to find an example.
这是我正在尝试做的:
$ cmake .
$ make
> Hello World! (Output)
在 Gnu 中使这很容易:
In Gnu Make that's very easy:
bar:
echo Hello World!
但我试图在 cmake 中做到这一点.根据我一直在阅读的内容,我应该可以使用下面的 CMakeLists.txt
文件来做到这一点:
But I'm trying to do this in cmake. Based on what I've been reading, I should be able to do that with the CMakeLists.txt
file below:
cmake_minimum_required(VERSION 3.6)
project(foo)
add_custom_target(bar)
add_custom_command(
TARGET bar
COMMAND "echo Hello World!"
)
如果我只是调用make
,目前没有工作要做.我需要显式调用 make bar
.如何将 bar
添加到 all
配方?
Currently there is no work to do if I just call make
. I need to explicitly call make bar
. How can I add bar
to the all
recipe?
我已经尝试添加 add_dependency(foo bar)
,但是 foo
是一个不存在的目标.如果有一些我不知道的超级目标,那就完美了.然后我就可以将它用作我的自定义命令的目标,而不用打扰 bar
.
I've tried adding add_dependency(foo bar)
, but foo
is a non-existent target. If there is some super-target that I'm unaware of that would be perfect. Then I could just use that as the TARGET for my custom command and not bother with bar
.
推荐答案
使用 ALL
选项来构建目标默认:
Use ALL
option for build the target by default:
add_custom_target(bar ALL)
这篇关于在 CMake 中默认构建自定义目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!