Reputation: 3
I'm using Microsoft Visual Studio 2022 Community edition.
I want to create two executables from the same project using conditional attributes.
Example:
using System;
public class MyClass
{
static void Main()
{
#if (PROGRAM1)
Console.WriteLine("Program 1 exclusive");
#endif
Console.WriteLine("Common part");
#if (PROGRAM2)
Console.WriteLine("Program 2 exclusive");
#endif
}
}
I don't want to define PROGRAM1
or PROGRAM2
in the code, but they should be defined when I click build solution.
Also if there's a way to do more stuff like output each executable to a different directory, I would be interested.
Why I want to do this is my current code check a boolean value on the command line at start and act slightly differently depending on that value. I would prefer to have two executables with slightly different code, but that doesn't have to check this boolean.
PS: I took a look at this answer first but it doesn't really answer my question https://stackoverflow.com/questions/975355/how-to-do-ifdef-in-c-sharp
Upvotes: 0
Views: 82
Reputation: 844
You can output multiple executables from a single build and you can save them to different folders and give them different file names. You can create extra configuration in addition to Debug and Release configurations that get created automatically for most projects. For each configuration you can set a different value of variables (in your case, PROGRAM1 or PROGRAM2). Once you create your configurations and set appropriate values of your variables for each configuration, you can build multiple configurations with one build command by using Batch Build feature of the Visual Studio.
To create a build configuration for a project:
To access Batch Build dialog, use the menu bar, Build > Batch Build.
To output your executables to different folder and file names, you can set the OutputPath or OutputDir variables for each configuration.
More details on build configurations is on Visual Studio Site
Upvotes: 1