Home > database >  Batch building the same code multiple times with different values for a variable in Visual Studio
Batch building the same code multiple times with different values for a variable in Visual Studio

Time:02-04

I need to build my code with a variable MyVar. I need to build it eg. 10 times but for each time, MyVar would be different, eg.

First build: static unsigned char MyVar[] = "ABC";

Second build: static unsigned char MyVar[] = "XYZ";

Is there an option to do this in a batch style? Eg. the command line? I just don't want to change MyVar manually, press "build", rename the built file etc.

Thanks a lot

PS: I'm not a professional coder, this could be bad practice. At the same time I'd just like to get the job done and don't want to change the code too much.

PPS: Already looked into several things (property sheets, custom macros, per-/post build actions, environment variables) but didn't find anything suitable.

CodePudding user response:

Here is a sample C program utilizing a macro named MY_MACRO that you'll set in the CL environment variable using the /D option, before invoking MSBuild:

#include <iostream>
#include <string>

#ifndef MY_MACRO
#define MY_MACRO "unknown"
#endif

std::string greeting(const std::string& their_name)
{
    static constexpr char my_name[] = MY_MACRO;
    return "Hello, "   their_name   ", nice to meet you!\nMy name is "   my_name   '.';
}

int main()
{
    std::cout << "Please enter your name: ";
    std::string their_name;
    std::cin >> their_name;
    std::cout << greeting(their_name) << '\n';
}

As described in the docs mentioned above, when dealing with environment variables, you'll use the number sign (#) instead of the equal sign (=) to define a preprocessor constant with an explicit value. Also, the double-quotes (") need to be escaped (like \") when defining a string constant via command line.

To sum it up, here is a sample PowerShell script to build and collect ten different executables, each of which will be using a different string literal for the value of MY_MACRO, selected from a list of names, in a loop (note that in the script, \" needs to be expressed as \`" using a backtick before double-quotes, so that PowerShell will interpret literally those double-quotes inside a double-quoted string):

$MSBuildExe = 'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe'
$MSBuildCmdLineArgs = @('MySolution.sln', '/property:Configuration=Release;Platform=x64', '/target:Rebuild', '/maxCpuCount')

New-Item -Force -ItemType Directory -Name 'CollectedExecutables'

$Names = @('Anni', 'Frida', 'Kadi', 'Mari', 'Piia', 'Pille', 'Piret', 'Reet', 'Siret', 'Triinu')
foreach ($MyNameString in $Names)
{
    $env:CL = "/DMY_MACRO#\`"$MyNameString\`""
    & $MSBuildExe $MSBuildCmdLineArgs "/property:TargetName=executable_named_$MyNameString"
    Copy-Item -LiteralPath "x64\Release\executable_named_${MyNameString}.exe" -Destination 'CollectedExecutables'
}

In this script, I'm assuming you're using Visual Studio 2022; otherwise just change the value of $MSBuildExe to the appropriate path. Also, I'm assuming you want to build a Release (not Debug) configuration of your C project, and that you want to build for the x64 platform. If not, change those strings in the script accordingly. And of course, put the actual name of your C solution file (instead of MySolution.sln) into the first element of the MSBuild command-line arguments array in the script.

  •  Tags:  
  • Related