5

I have a solution with multiple projects. One project only needs to build if both two events, in the pre-build event, exit with error code 0.

So I thought I could do the following:

"C:\Path\To\Binary1.exe" & "C:\path\to\binary2.exe"

In my test scenario something goes wrong so Binary1.exe exits with a non-zero value. But visual studio goes on building the project anyway. When I run the pre-build event commandline in cmd and echo %errorlevel% I see the exit code being non-zero.

When I only put

"C:\Path\To\Binary1.exe"

in the pre-build event, the build is stopped and en error is shown in the Error List window of Visual Studio.

I am definitely sure that Binary1.exe is exiting with a non-zero value as its also shows a messagebox prior to exit.

I can think of one solution. Binary1.exe calling Binary2.exe and exiting with a non-zero exit code when Binary2.exe exits with a non-zero exit code. But that is not really a flexible solution.

To summarize: How can I run multiple pre-build events and stop buidling when one of the commands returns a non-zero value?

Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76

2 Answers2

4

I think yuou can do as follows:

run command 1
if ERRORLEVEL 1 (
    exit /b 1
    )
run command 2
andreikashin
  • 1,528
  • 3
  • 14
  • 21
  • or you can check the command shell environment variable like this `if %errorlevel% == 0 ( Do something)`. – RBT Nov 28 '17 at 05:00
2

If the two projects are in the same solution, you can set the dependency in Visual studio. Right-click on the solution in the solution explorer and choose "Project Dependencies".

Set the 'last' project to be depending on the first two. In this case Visual studio will build in the right order and will stop building if one of the dependencies fail to build. (Visual Studio 2013)

Sammy
  • 36
  • 4
  • Its not about project dependencies. Its about a pre-build event, that should fail when either one of the executables called in the pre-build event fails. My question is more about pre-build event syntax (or cmd syntax in general?). – Mike de Klerk Jun 25 '14 at 06:37