Plotting Events in a C ++ Project

I have a post-build event in my C ++ Visual Studio 2010 project that is using the command xcopy

, but when this xcopy return error code (> 0), all builds failed too, and the message "build unsuccessfull" how can I turn sensitivity to bugs in build events?

Thank!

+3


source to share


2 answers


You can use the task Exec

IgnoreExitCode

:



<Target Name="MyAwesomePostBuildTarget" AfterTargets="Build">
  <Exec IgnoreExitCode="true" Command="xcopy etc. etc." />
</Target>

      

+5


source


You can override the failure of (almost) any batch CMD by appending || exit /b 0

to the end of the command. Example:

del somefile.txt || exit /b 0

Thus, batch files work a bit like C. You can do && to conditionally run a command when the previous command is successful, and || to run the command when the previous command failed.

exit /b 0

tells the CMD processor to exit the script and set the error level to zero (0). Never forget to turn on the / b switch! Without it, the CMD will exit the calling script as well as the current script, which rarely, if ever, has the desired behavior.



I am using this trick from Visual Studio IDE, so there is no need to do low level hacking. And it fits in one line, which is also convenient from the IDE.

Another useful trick is command silence, by the way:

xcopy srcfile destfile 1>nul 2>nul || exit /b 0

1

is stdout and 2

is stderr. Windows shell packages are notoriously incompatible with what kind of output they might use, so I usually just concatenate both or nowhere.

+1


source







All Articles