13

How do I run a .bat file from CMake in a pre-link or a post-build event?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
marius c
  • 153
  • 1
  • 1
  • 4

2 Answers2

17

You could use add_custom_command, e.g.

if(WIN32)
  add_custom_command(TARGET <Your target>
                       POST_BUILD
                       COMMAND cmd //C <path to .bat file> <ARGS> )

endif()


For full details about add_custom_command run

cmake --help-command add_custom_command
Community
  • 1
  • 1
Fraser
  • 74,704
  • 20
  • 238
  • 215
6

The following also works. In case you read or create a file inside the bat script don't forget to specify the exact path inside the bat script.

ADD_CUSTOM_TARGET(
    myCustomTarget
    COMMAND cmd /c E:/Myfiles/mytxt.bat
)
ADD_DEPENDENCIES(myTarget myCustomTarget)

myTarget will be executed after myCustomTarget.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vsegar
  • 113
  • 1
  • 8