add_custom_command不生成目标 [英] add_custom_command is not generating a target
问题描述
也许这是不可能的,我误读了 cmake 3.2文档,但是我虽然创建一个自定义命令将在Makefile中创建一个自定义目标,以便我可以通过调用输出文件的名称来构建目标。 CMake文档说:
Perhaps this is impossible and I'm misreading the cmake 3.2 documentation, but I though creating a custom command would create a custom "target" in the Makefile so that I could build the target by invoking the name of the output file. The CMake docs says:
在makefile术语中,这将创建一个新的目标,形式如下:
In makefile terms this creates a new target in the following form:
OUTPUT: MAIN_DEPENDENCY DEPENDS
COMMAND
所以我想我可以运行 make OUTPUT
。也许该文档使CMake目标与Makefile目标混淆?
so I thought I could then run make OUTPUT
. Perhaps the documentation is confusing CMake targets with Makefile targets?
例如,
add_custom_command(OUTPUT foo_out
COMMAND post_process foo_in > foo_out
DEPENDS foo_in
)
我想做
make foo_out
,它会使 foo_out
。但是,如果我这样做,我得到
and it will make foo_out
. However, if I do this, I get
make: **** No rule to make target `foo_out`. Stop.
并且肯定的,单词foo_out不存在于cmake二进制文件中的任何文件输出目录。如果我将其更改为
and sure enough, the word "foo_out" doesn't exist anywhere in any file in the cmake binary output directory. If I change it to this
add_custom_target(bar DEPENDS foo_out)
add_custom_command(OUTPUT foo_out COMMAND post_process foo_in > foo_out)
然后我可以做
make bar
我可以做
make foo_in
仍然无法做到
make foo_out
make bar
的问题是它不直观,因为实际的文件输出是 foo_out
不 bar
。
The problem with make bar
is that it is unintuitive, as the actual file output is foo_out
not bar
.
我如何做?
在我的例子中,我需要对标准可执行目标运行一个特殊的处理步骤,将可选资源插入到ELF文件中。我想要有两个可执行文件作为Makefile目标的能力,所以我可以构建裸体ELF可执行文件以及注入资源的ELF可执行文件。
In my case, I need to run a special processing step to the standard executable target which inserts optional resources into the ELF file. I would like the ability to have both executables as Makefile targets, so I can build the naked ELF executable as well as the resource-injected ELF executable.
如果我写一个自定义Makefile,这是无关紧要的!
If I was writing a custom Makefile, this is trivial to do!
foo_in: foo.c
$(CC) $< -o $@
foo_out: foo_in
post_process $< > $@
我可以做 make foo_in
和 make foo_out
。
推荐答案
add_custom_command
不创建新目标。您必须通过 add_executable
, add_library
或 add_custom_target
add_custom_command
does not create a new target. You have to define targets explicitly by add_executable
, add_library
or add_custom_target
in order to make them visible to make.
如果您需要修复部署,可以
If you have to fix things up for deployment, you could
1。使用 install
命令(在CMakeLists.txt中的某个位置),如下所示:
1. use the install
command (somewhere in your CMakeLists.txt) like this:
install(SCRIPT <dir>/post_install.cmake)
$ b b
存储只有在单独的 .cmake 文件中运行 make install
时才执行的命令。或者,如果安装目标已预留给其他项目,或者您有更复杂的工作:
to store commands which are executed only when you run make install
in a separate .cmake file. Or if the install target is already reserved for other things or you have more complex stuff going on:
2。手动定义部署目标。一旦你得到了,你可以创建一个自定义后生成命令,只有当你明确地运行make在你的部署目标。
2. manually define a deploy target. Once you got that, you can create a custom post-build command which is only executed when you explicitly run make on your deploy target. This allows you to execute commands through a separate target.
在您的CMakeLists.txt中,这可能如下所示:
In your CMakeLists.txt this could look like:
cmake_minimum_required(VERSION 3.0)
add_executable("App" <sources>)
# option 1: do deployment stuff only when installing
install(SCRIPT <dir>/post_install.cmake)
# option 2: define a deploy target and add post-build commands
add_custom_target("deploy")
add_custom_command(TARGET "deploy" POST_BUILD <some command>)
单独的开发从昂贵的现成部署构建(如果我理解正确,这是这里的目标)。我会推荐选项1,因为它只是更干净。
Both approaches allow you to separate dev builds from expensive ready-to-deploy builds (if I understand correctly, that's the goal here). I would recommend option 1 since it's just cleaner.
希望这有助于!
这篇关于add_custom_command不生成目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!