Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing csproj OutputType based on project configuration

I need to build a C# project as either WinExe or Library depending on the project's configuration.

I've tried both of these methods with no luck:

1) In the general PropertyGroup:

<OutputType Condition=" '$(Configuration)' == 'Release' ">WinExe</OutputType> <OutputType Condition=" '$(Configuration)' == 'Debug' ">Library</OutputType>

2) In a conditional PropertyGroup:

<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> <OutputType>WinExe</OutputType> </PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <OutputType>Library</OutputType> </PropertyGroup>

Neither of these methods work and the OutputType is always WinExe. The odd thing is that if I change all instances of WinExe to Library, then it's always Library. This is making me think that it is reading them successfully, but either in a strange order or that WinExe takes precedence over Library.

Any ideas?

like image 934
Guy Danus Avatar asked Jul 28 '11 22:07

Guy Danus


People also ask

How do I change the output type to a class library?

In the Solutions Explorer window, right-click the exe project and then select Properties in the popup. Then change the Output type from Windows Application to Class Library.

What is IntermediateOutputPath?

The IntermediateOutputPath. The full intermediate output path as derived from BaseIntermediateOutputPath, if no path is specified. For example, \obj\debug. If this property is overridden, then setting BaseIntermediateOutputPath has no effect. You can read this up here.

How is Csproj file created?

Developers compile CSPROJ files using MSBuild (the Microsoft Build Engine). When developers create a new program in Microsoft Visual Studio, they start by creating a new project and associated project file.


1 Answers

In the top of your .csproj file you will have two sections that look a bit like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
  <OutputType>Library</OutputType>
  <!-- Other properties go here -->
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  <OutputType>Exe</OutputType>
  <!-- Other properties go here -->
</PropertyGroup>

Add your OutputType elements to these two conditional PropertyGroup sections and make sure that you remove all other OutputType elements - I've just tested it and it does exactly what you are asking for.

Yes this is very similar to what you have already done, but I know for a fact that the above method works because I've just tried it - my only guess is that something somewhere else in your build is messing things up.

like image 166
Justin Avatar answered Oct 17 '22 09:10

Justin