Skip to content

Pre/Post Build Events in Visual Studio

Last updated on April 1, 2019

Introduction

Pre/Post Build Events are useful when we wish to perform some operation before/after a project is build. These operations are nothing but the shell commands being used from the command line.

Consider a scenario where we build our library project and its .dll is saved into the Project/bin/Release directory. For some reason we want all our library files in one common directory that we would refer somewhere in our project.

One way to do it, is that we manually copy the library files from their respective release or debug directories into the desired directory. Just kidding, we obviously don’t wanna do that. A preferred way to do it, is by using Pre/Post Build Events.

Pre/Post Build Events
Pre/Post Build Events

Event Macros

We use post build events when we wish to perform an operation after the build is successful. Pre build events, on the other hand, are used when we want an operation to be performed before the build starts. These build events are composed of the shell commands similar to the following one:

copy "$(TargetDir)*$(TargetExt)" "$(ProjectDir)..\Library\"

 

Assume that above line of code is a post build event. The event will copy the targeted files from the project directory into the Library directory, which resides in the main directory. Lets have a closer look at the command line event in parts:

  • copy is the shell command that we wish to execute when our build is successful. Similarly, we can use other commands as well. For example, we can use move instead of copy.
  • $(TargetDir) returns the value of TargetDir macro, which is nothing but a directory path. This is the directory from where we wish to extract our files. Visual Studio gives us a list of macros that we can use while creating our build event command. Click here to know more about the macros available.
  • *$(TargetExt) returns the list of all files that have the TargetExt. If the value of TargetExt is not what we want, we can simply replace the macro with our own extension. It is because of the * that we get all the files. If we know the file name we can use the file name with the target extension and that file will be copied to the desired location.
  • $(ProjectDir) gives us the project directory path. In the sample command, I have used the project directory, however we can always replace it with other available macros as per our need.
  • Because I want to store my desired files into the Library directory which lies in the main directory, I have used ” ..\” followed by the directory name Library.
  • When the build event is triggered, we can see the actual command executed by Visual Studio in the background, in its output window.

We can execute multiple commands in our build event. All we need, is to add a new line between the two commands. Not only this, we can also execute commands based on some condition. In order to achieve it, we use the if operator as shown in the following example :

move "$(ProjectDir)..\Library\*$(TargetExt)" "$(ProjectDir)..\Test\"
if $(ConfigurationName) == Release copy "$(TargetDir)*$(TargetExt)" "$(ProjectDir)..\Library\"

In the above event, we are executing two commands. First one, moves the targeted files from Library directory into the Test directory. The second command is, however, a conditional command which executes only if the value of $(ConfigurationName) is Release.

 

Conclusion

While writing my own first build event, I found it a bit difficult to understand the parts of the command. I hope this post helps you get a better understanding of the same. It’s always great to have feedback from the readers. Your valuable feedback, question, or comments about this article are always welcome.

Published inCS Fundamentals