views:

53

answers:

1

In a Visual Studio 2010 C++ project file, is it possible to use conditionals to determine the presence of a library, and alter preprocessor flags, etc appropriately?

To be more concrete, say we have a directory C:\libraries\MKL, i would like to #define MKL and add mkl_dll.lib as an additional dependency if that directory exists.

Previously we have used multiple solution configurations to achieve this, but that is quite hard to maintain.

+1  A: 

The following, when pasted into the bottom of an F# project, has the suggested effect (if c:\temp\foo.txt exists, then a #define for THE_FILE_EXISTS is added). I expect that only minor modifications would be needed for a C++ project, since they both use MSBuild. This is a little hacky maybe, it is the first thing I got working.

<UsingTask TaskName="SeeIfFileExists" TaskFactory="CodeTaskFactory" 
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <Path ParameterType="System.String" Required="true" />
    <ItExists ParameterType="System.Boolean" Output="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs">
      <![CDATA[
ItExists = System.IO.File.Exists(Path);
]]>
    </Code>
  </Task>
</UsingTask>
<Target Name="SeeIfFileExistsTarget" BeforeTargets="PrepareForBuild">
  <SeeIfFileExists Path="c:\temp\foo.txt" >
    <Output TaskParameter="ItExists" ItemName="TheFileExists" />
  </SeeIfFileExists>
  <PropertyGroup>
    <DefineConstants Condition="'@(TheFileExists)'=='True'"
        >$(DefineConstants);THE_FILE_EXISTS</DefineConstants>
  </PropertyGroup>
</Target>

It just occurred to me that

<PropertyGroup>
    <DefineConstants Condition="Exists('c:\temp\foo.txt')"
        >$(DefineConstants);THE_FILE_EXISTS</DefineConstants>
</PropertyGroup>

is probably sufficient, but not nearly as sexy.

Brian
thanks for that. I couldn't get the first version to work in the end, but the second one works a treat.
ngoozeff