Use CMake to run a C++ program post-build

亡梦爱人 提交于 2020-01-01 05:54:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/18961650/use-cmake-to-run-a-c-program-post-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!