How do I make different configurations have different dependancies?

Answered How do I make different configurations have different dependancies?

  • Saturday, March 31, 2012 5:14 PM
     
     

    I'm using VS10.  I've set up a Solution to use multiple LIBs, and specified everything using project-to-project dependencies with no explicit paths or library names in the linker properties.  All is good.

    But now we want to build a variation, with some code changed out.  This means swapping one LIB with another (a DLL actually), and my thought was to set a different Configuration for that build.  But the project-to-project dependencies are global and don't apply to a specific configuration.

    I've seen similar solutions posted that needed manual editing of the XML file, so I'm wondering if there are similar tricks that might apply here.  Or, is there a better way to set up what I'm looking for?

    —John

All Replies

  • Saturday, March 31, 2012 5:58 PM
     
      Has Code

    Maybe #pragma comment can be usefull for you. That would allow you to put library names in the source code instead of in the linker properties.

    #ifdef MY_CONDITION
    #pragma comment(lib, "mylib1.lib")
    #else
    #pragma comment(lib, "mylib2.lib")
    #endif

  • Saturday, March 31, 2012 7:00 PM
     
     
    True, but it would not help with the dependency on building the lib for the proper configuration.
  • Monday, April 02, 2012 6:47 AM
    Moderator
     
      Has Code

    Hi John,

    You need to modify Project File (VCXproj) using MSBuild script. In MSBuild, we can set condition for specific configuration. for example:

     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
        <ClCompile>
          
        </ClCompile>
        <Link>
               <GenerateDebugInformation>true</GenerateDebugInformation>
          <AdditionalDependencies>B.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
      </ItemDefinitionGroup>
      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
        <ClCompile>
              </ClCompile>
        <Link>
               <GenerateDebugInformation>true</GenerateDebugInformation>
          <EnableCOMDATFolding>true</EnableCOMDATFolding>
          <OptimizeReferences>true</OptimizeReferences>
          <AdditionalDependencies>A.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
      </ItemDefinitionGroup>

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, April 04, 2012 2:35 AM
     
      Has Code

    Hi John,

    You need to modify Project File (VCXproj) using MSBuild script. In MSBuild, we can set condition for specific configuration. for example:

     <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
        <ClCompile>
          
        </ClCompile>
        <Link>
               <GenerateDebugInformation>true</GenerateDebugInformation>
          <AdditionalDependencies>B.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
      </ItemDefinitionGroup>
      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
        <ClCompile>
              </ClCompile>
        <Link>
               <GenerateDebugInformation>true</GenerateDebugInformation>
          <EnableCOMDATFolding>true</EnableCOMDATFolding>
          <OptimizeReferences>true</OptimizeReferences>
          <AdditionalDependencies>A.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
      </ItemDefinitionGroup>

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Er, isn't that just the same as using the Additional Dependencies item in the Linker->Input property page?

    As I indicated in my initial posting, this does not work as well as project-to-project dependencies because I will also need to explicitly set up Additional Library Directores to match (instead it just knowing about the other project), and will not set different built-time dependencies for each configuration. 

  • Wednesday, April 04, 2012 3:05 AM
    Moderator
     
     Answered Has Code

    HI,

    You are going to set the different project-to-project dependencies by the configuration. If I understand correctly, you can do the following by modifing the project msbuild script. 

    For example, we have a c++ project contains one main exe project, and two DLL projects, called dll1 and dll2. We want the project to reference the dll1 project for the debug configuration and dll2 for release configuration. You can use condition for the reference defination ItemGroup.

    Unload the project the open the project file using edit mode. You need to find the ProjectReference Section for the project references and append the Condition section as following. 

     <ItemGroup>
        <ProjectReference Include="..\dll1\dll1.vcxproj" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
          <Project>{b018dfe9-0942-47d2-9041-77de9214d66b}</Project>
        </ProjectReference>
        <ProjectReference Include="..\dll2\dll2.vcxproj" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
          <Project>{a0efeb3a-de40-47a0-bb64-5f41d75e815e}</Project>
        </ProjectReference>
      </ItemGroup>

    If I misunderstand you, please let me know.

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, April 04, 2012 6:05 AM
     
      Has Code

    I think that's exactly what I was looking for; thanks.

    So where it says ProjectReference for the thing I want to make used on some configurations only, I can add the Condition attribute.  Here is one from my project:

        <ProjectReference Include="..\..\foo\bar\DLL1.vcxproj">
          <Project>{559cbdd3-581c-4337-b1c5-97ca1310054a}</Project>
          <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
          <Private>false</Private>
          <CopyLocalSatelliteAssemblies>false</CopyLocalSatelliteAssemblies>
          <LinkLibraryDependencies>true</LinkLibraryDependencies>
          <UseLibraryDependencyInputs>false</UseLibraryDependencyInputs>
        </ProjectReference>

    I could add the attribute to the ProjectReference opening tag, something like

    Condition= "'$(Configuration)'=='Debug-old-way'"

    Assuming I'm following thus far, what I really want is to match both Debug-old-way and Release-old-way (and a different one will match Release-new-way and Debug-new-way).  Is there a wildcard string match available?

    —John


  • Wednesday, April 04, 2012 8:15 AM
     
     

    From what I've seen of actual VC project files, the condition always lists the configuration/platform pairs for every configuration and platform combination available even if the settings are the same.

    If you want to confirm this for yourself though you should look in the MSDN for the MSBuild project schema and see if it lists other forms to Condition that are acceptable.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • Monday, April 09, 2012 6:24 AM
    Moderator
     
     

    Hi John,

    Please take a look this one: http://msdn.microsoft.com/en-us/library/7szfhaft.aspx. In condition, we can use function "exist" to check if the string contains a sub string and we can use other logical combinations.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, April 16, 2012 2:43 AM
    Moderator
     
     

    Hi,

    May I know the situation on your side? Is the issue resolved?

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, April 24, 2012 11:30 AM
     
     

    Hi,

    May I know the situation on your side? Is the issue resolved?

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Tabled for now.  When we merge the work into the development solution, I'll look at the "Condition" edited outside of the IDE.