Multiple Pre-Build Events in Visual Studio?

心已入冬 提交于 2019-11-29 02:59:41

Turns out the problem is Scott's example doesn't include the call command at the start of the line. This is fine as long as you don't want to execute the .bat file multiple times with different parameters.

This:

call "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)connectionStrings.config.$(ConfigurationName)" "$(ProjectDir)connectionStrings.config"
call "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)appSettings.config.$(ConfigurationName)" "$(ProjectDir)appSettings.config"

worked fine for me.

I'm using Visual Studio 2010 on Windows 7 64-bits.

I found a solution that's better to my liking, I'm chaining commands together with && ^, for example, editing my .vcxproj xml file like so:

    <PreBuildEvent>
      <Command>echo "hello 1" &amp;&amp; ^
echo "hello 2" &amp;&amp; ^
echo "hello 3"</Command>
      <Message>Performaing pre-build actions ...</Message>
    </PreBuildEvent>

I've chained three commands together. The '&&' tells the shell to stop executing if the previous command errors out.

Note, in the .xml file, one can't use && directly, so '& amp;& amp;' must be used instead. Also note the use of the ^ character, this is the cmd shell's line continuation character (like BASH's \ char).

Each new command must start at the beginning of the line. Using this approach, one can chain as many commands as one wants, but if any fail, the rest in the chain won't get executed.

Here's my actual usecase, I'm performing some environment checks with a Python script, followed by copying my pre-compiled header's debug file to another sub-project's directory:

    <PreBuildEvent>
        <Command>C:\Python27\python.exe "$(SolutionDir)check_path.py" 32 &amp;&amp; ^
copy "$(SolutionDir)h1ksim_lib\vc$(PlatformToolsetVersion).pdb" "$(IntDir)" /-Y > nul</Command>
        <Message>Performaing pre-build actions ...</Message>
    </PreBuildEvent>

The > nul usage on the copy command prevents any output showing up in the build window, so my teammates won't get freaked out when it says 0 file(s) copied.

Reference to sharing pre-compiled headers between sub-projects is here:

Sharing precompiled headers between projects in Visual Studio

You should be able to put the commands on a separate line, and then each command will be executed in sequence. Alternatively, a "cheater" way to do the same thing is as follows:

  • Create a second batch file that runs the desired portion of new functionality, then create a parent batch that runs the two component batch files, and run that new batch in your pre-build event. For example: batch1.bat modifies file 1, batch2.bat modifies file 2, then batch3.bat runs both batch1.bat and batch2.bat
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!