使用CMake运行一个C ++程序后构建 [英] Use CMake to run a C++ program post-build

查看:193
本文介绍了使用CMake运行一个C ++程序后构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的应用程序,我使用CMake来构建和发布二进制文件。



我想要编译CMakeLists.txt脚本运行用于在许可证文件构建应用程序的二进制文件之后对其进行时间戳和加密的CPP文件。我看到了运行 execute_process 命令的示例,例如:

  execute_process(COMMANDgcc -o foo foo.cpp
WORKING_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR})
execute_process(COMMAND./foo
WORKING_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE MY_FOO_VAR )

我使用Visual Studio 2010在Windows上构建。问题是,我不知道如何获得CMake在VS的构建过程中运行一个程序。我想在构建操作的最后一件事是许可证文件时间戳,但我真的不知道VS或CMake得到这个设置。

execute_process()在CMake时间运行,不是在构建时。您要查找的是 add_custom_command()

  add_executable (LicenseStamper stamper.cpp)

add_custom_command(
OUTPUT stamped_file.lic
COMMAND LicenseStamper任何其他参数
DEPENDS any / dependency.file
COMMENT许可
VERBATIM


add_custom_target(
StampTheLicense ALL
DEPENDS stamped_file.lic

自定义命令将运行可执行文件(如有必要,先构建它)。自定义目标将驱动自定义命令 - 它取决于命令的输出,因此,当构建目标时,将需要构建其依赖关系,导致自定义命令运行。


I have an application that is written in C++ that I use CMake to build and release binaries.

I'd like to have the CMakeLists.txt script compile and run a CPP file that is used to timestamp and encrypt a license file after it has built the binaries for the application. I've seen examples of running the execute_process command such as this:

execute_process(COMMAND "gcc -o foo foo.cpp"
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
execute_process(COMMAND "./foo"
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                RESULT_VARIABLE MY_FOO_VAR)

I am using Visual Studio 2010 to do the build on Windows. The problem is that I'm not sure how to get CMake to run a program during VS's build process. I want the last thing in the build operation to be the license file timestamp, but I don't really know enough about VS or CMake to get this set up.

解决方案

You say you want to run the file after your build. execute_process() runs at CMake-time, not at build time. What you're looking for would be add_custom_command():

add_executable(LicenseStamper stamper.cpp)

add_custom_command(
  OUTPUT stamped_file.lic
  COMMAND LicenseStamper any other arguments
  DEPENDS any/dependency.file
  COMMENT "Stamping the license"
  VERBATIM
)

add_custom_target(
  StampTheLicense ALL
  DEPENDS stamped_file.lic
)

The custom command will run the executable (and build it first, if necessary). The custom target will drive the custom command - it depends on the command's output, so when the target is built, it will require its dependency to be built, causing the custom command to run.

这篇关于使用CMake运行一个C ++程序后构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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