Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 Compiling with the Debug or Release version of third party library depending on if my project is being compiled Build or Release?

I've downloaded a number of 3rd party libraries (dlls) now for Visual Studio 2010/C# and I've noticed that in their distributions \bin directory they usually have two versions Debug and Release.

Is there a way to add these libraries as references to the project, but use the Release build (when I'm building a release), and use the Debug build (when I'm debugging)?

like image 520
leeand00 Avatar asked Mar 30 '11 19:03

leeand00


People also ask

What is the difference between release mode and debug mode in Visual Studio?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.

What is debug build and release build?

Debug build and release build are just names. They don't mean anything. Depending on your application, you may build it in one, two or more different ways, using different combinations of compiler and linker options.

What is the difference between build and Release in Visual Studio?

Visual Studio projects have separate release and debug configurations for your program. You build the debug version for debugging and the release version for the final release distribution. In debug configuration, your program compiles with full symbolic debug information and no optimization.


3 Answers

<Reference Include="MyLib">
   <HintPath>..\lib\$(Configuration)\MyLib.dll</HintPath>
</Reference>
like image 65
WaffleSouffle Avatar answered Oct 10 '22 10:10

WaffleSouffle


You can edit the csproj file manually set the Condition attribute on the ItemGroup containing the reference.

  <ItemGroup Condition="'$(Configuration)' == 'Debug'">
    <Reference Include="MyLib">
      <HintPath>..\..\Debug\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Reference Include="MyLib">
      <HintPath>..\..\Release\MyLib.dll</HintPath>
    </Reference>
  </ItemGroup>

See this article for a bit more information.

like image 39
PHeiberg Avatar answered Oct 10 '22 11:10

PHeiberg


The answer by WaffleSouffle is definitely the best if you use a Release- and a Debug-folder, as the original question states.

There seems to be another option that is not so obvious because VS (VS2010) does not show it in the IntelliSense when editing the csproj-file.

You can add the condition to the HintPath-element. Like this:

<Reference Include="MyLib">      
      <HintPath Condition="'$(Configuration)'=='Release'">..\lib\MyLib.dll</HintPath>
      <HintPath Condition="'$(Configuration)'=='Debug'">..\lib\Debug\MyLib.dll</HintPath>
</Reference>

I found an article by Vivek Rathod describing the above approach at http://blog.vivekrathod.com/2013/03/conditionally-referencing-debug-and.html.

I checked the XMS Schema file for the project file at: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Core.xsd and: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd

I cannot see that Condition is a supported attribute for the HintPath-element, but it does seem to work.....

EDIT 1: This does not make the reference show up twice in Visual Studio which is an issue with the accepted answer.

EDIT 2: Actually, if you omit the HintPath alltogether Visual Studio will look in the projects output folder. So you can actually do this:

<Reference Include="MyLib">        
     <!-- // Removed HintPath, VS looks for references in $(OutDir) --> 
</Reference> 


The search order is specified in the file Microsoft.Common.targets
See: HintPath vs ReferencePath in Visual Studio

like image 13
Nils Lande Avatar answered Oct 10 '22 11:10

Nils Lande