add_custom_command 未生成目标 [英] add_custom_command is not generating a target

查看:25
本文介绍了add_custom_command 未生成目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是不可能的,我误读了 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.

果然,cmake 二进制输出目录中的任何文件中都不存在foo_out"这个词.如果我改成这个

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_inmake foo_out.

推荐答案

add_custom_command 不会创建新目标.您必须通过 add_executableadd_libraryadd_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)

将仅在您运行 make install 时执行的命令存储在单独的 .cmake 文件中.或者,如果安装目标已经为其他事情保留,或者您有更复杂的事情要做:

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. 手动定义一个部署目标.获得该命令后,您可以创建一个自定义的构建后命令,该命令仅在您在 deploy 目标上显式运行 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屋!

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