3

I'm trying to compile an #ifdef'd codebase to two different target frameworks, namely 3.5 and 4.0.

I attempted to modify the .proj files in the solution to no avail.

It seems MSBuild / VS2012 isn't picking up the solution configuration change via UI.

This is a fragment of one of the .proj files:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>..\Binaries\</OutputPath>
    <DefineConstants>TRACE;NET35</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 40|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>..\Binaries\</OutputPath>
    <DefineConstants>TRACE;DEBUG;NET40</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
  </PropertyGroup>

When I switch to "Debug 40" from "Release" the target framework does not change in the project properties (thus breaking the compilation because of other conditionally referenced assemblies).

I'm also having problems conditionally referencing different assemblies, as if, again, the solution configuration isn't picked up by VS/MSBuild (some don't even show up in the references).

Edit: I'm excluding Microsoft.CSharp from the v3.5 build with the following line:

<Reference Include="Microsoft.CSharp" Condition=" '$(Configuration)' == 'Debug 40'" />

So far it seems it's just the TargetFrameworkVersion property that is ignored.

raine
  • 817
  • 1
  • 15
  • 26
  • Are you sure that's not just a VS GUI issue as when i made my debug 3.5 version it failed to compile against 4.0 csharp dll (as youd expect). Id check the built dll with reflector. – James Woolfenden Sep 03 '13 at 09:50
  • @JamesWoolfenden I'm conditionally referencing Microsoft.Csharp as well. I'll edit the question. – raine Sep 03 '13 at 10:03
  • 1
    Hi Raine. Thats my point i think the right framework is being used otherwise why would it fail. I think its just the ui not updating. If you build it using msbuild at the commandline you will see which framework is being used in the CoreCompile task – James Woolfenden Sep 03 '13 at 10:52
  • hello @JamesWoolfenden, thanks for your help! I have noticed that in one project it's just Resharper highlighting errors, but the compilation is fine. In another project MSBuild seems to be ignoring some conditional references. I'm unable to pinpoint the exact issue right now. – raine Sep 03 '13 at 12:22
  • 2
    Best writeup I've come across. http://alandean.blogspot.com/2011/02/framework-version-targeting-with.html I do not like the way you have the same OutputPath for both, BTW. – granadaCoder Sep 03 '13 at 13:47
  • @granadaCoder I don't like it either, I'll be setting different output paths right away. thanks for the advice. – raine Sep 04 '13 at 09:39

1 Answers1

2

Turns out the problem was twofold:

  • ReSharper (v7) making things harder by displaying compilation errors across different files, making it harder to realize that code actually compiled with conditional references. I think version 7 does not support manual project file changes, so an heads up to RS7 users.
  • VS2012 not changing the target framework in the UI (despite actually using the correct framework when compiling, as seen in the verbose output of MSBuild).

I eventually managed to get the codebase to compile with a bit of patience. As per @granataCoder's suggestion, it's also better to keep different output paths (might be easy to overlook when dealing with orthogonal issues like conditional compilation).

raine
  • 817
  • 1
  • 15
  • 26