How to create two different executables from one Visual Studio project

Gevo12321 picture Gevo12321 · Nov 18, 2015 · Viewed 18.5k times · Source

I have a main executable that runs based on settings saved in a configuration file. I want to be able to change the settings in the config file through a different executable.

Is there an easy way of building these two different executables in one Windows Forms project? Meaning that when I press build, two different EXE files get created in the same solution folder - the one that changes the configuration file, and the other that uses it.

I know that this is possible to do if I create two separate projects that use the same solution folder, but I was hoping to do it all in one step.

I am assuming that to do this, I need a project with two "Main" functions. Is this possible?

Answer

Marc picture Marc · Nov 18, 2015

You can build as many assemblies in one solution as you like. Assemblies can result in DLL files or EXE files.

Create a solution (or open an existing solution).

  1. Right-click the root node in Solution Explorer and choose AddNew Project and choose the project type you like to add.

  2. Right-click the project item in Solution Explorer and choose PropertiesBuildOutput path. Set to the desired directory where to build it to. Repeat this for the other projects.

This way you get the following in Solution Explorer:

  • MySolution
    • MyCommonCode (Class Library, results in MyCommonCode.dll)
    • MyMainApp (Windows Forms application, results in MyMainApp.exe)
    • MyConfigApp (Windows Forms application, results in MyConfigApp.exe)

The MyCommonCode assembly contains shared code that both EXE files are using like the identifiers of your configuration file, etc.

MyMainApp is the GUI application (Windows Forms, WPF, etc.) for your main application with a project-reference to the MyComonCode project.

MyConfigApp is a GUI application for editing the configuration values with a project reference to MyCommonCode project.

After building your solution you get the following binaries: MyCommonCode.dll, MyMainApp.exe, and MyConfigApp.exe.

Update based on the comment:

One compile-run can build only one binary (DLL or EXE) per project. You can do something like the answer above: move most of the code in a common/core DLL and make two thin projects for the two EXE files which only "configure and use" the central common/core DLL file.

You can build different EXE files based on the same project using compiler defines. You can even define your own defines. But per compile-run you can only build one binary (DLL, EXE) per project - one or the other, but not both.