Visual Studio project build as an executable AND a DLL

26,178

Solution 1

If I'm not mistaken, you can use the EXE as a class library.
Just add a reference to it, in the other projects. A .NET EXE is just an assembly.

Solution 2

You could build the dll by default, and make another dependant target that is simply a wrapper console app that uses the dll.

Solution 3

Create two separate projects, one for your Console app and one for the Class library. Set the appropriate Output type for each.

Don't forget to add a reference to your Class library to your Console app project though.

Solution 4

I think the simplest solution is to build as a EXE and then have a post build action which copies the EXE into a DLL. There is no real difference between the two in .Net.

Solution 5

You cannot compile to both an exe and a dll. Whether an assembly is treated as an exe or a dll is determined by a single bit flag in the portable executable header of the file (see http://msdn.microsoft.com/en-us/magazine/cc301805.aspx for more info). This flag cannot have both values.

What you can do to satisfy your need is to add a reference to your exe. You cannot do this in some versions of Visual Studio (2005 and below) as the UI will not let you, but you can hand edit the project file to add the reference. Later versions of Visual Studio do allow you to add references to exe files using the UI.

Share:
26,178
East of Nowhere
Author by

East of Nowhere

Updated on July 25, 2020

Comments

  • East of Nowhere
    East of Nowhere almost 4 years

    In Visual Studio 2008 project properties, Application tab, I can set the Output type to Windows Application, Console Application, or Class Library. I have a project that I want to build as a stand-alone tool (console app) and be available to a couple other tools I'm working on as a class library.

    The VS GUI only lets me choose one or the other, and building the proj twice all the time is inconvenient.

    How can I set it up to build both output types in a single build job? Do I write some custom MSBuild .targets file or what?

  • East of Nowhere
    East of Nowhere about 14 years
    That bit flag means that a single file cannot be both a DLL and an .exe, that's obvious. But a .NET build can't have more than one build target? That sounds fishy to me, and if it's true it'd be a huge limitation of MSBuild (so maybe I'd need to learn NAnt...)