Kendall Frey
Kendall Frey

Reputation: 44316

Force Visual Studio to rebuild dependent projects

How can I force Visual Studio 2012 to rebuild all dependent projects when a project changes?

I have two projects: a C++ DLL, and a C# WPF app. The C# project has a post-build command to copy the DLL into the Debug folder.

When I modify the C++ project, the DLL is rebuilt, but the C# project isn't, and so the DLL isn't updated in the Debug folder. Then I need to clean and rebuild the solution before I see the updated results.

I'd like to tell VS that whenever I update the C++ project, it should rebuild the C# project (or at least run the post-build command). The C# project is dependent on the C++ project, but not through a reference.

Upvotes: 8

Views: 8237

Answers (3)

tm1
tm1

Reputation: 1319

Insert the following code into each dependent project:

<ItemGroup>
    <None Include="$(MSBuildProjectDirectory)\Properties\Build\_buildforcer">
        <Visible>true</Visible>
    </None>
</ItemGroup>
<Target Name="ForceNextBuild"
        AfterTargets="PrepareForRun"
        Condition=" '$(BuildingInsideVisualStudio)' == 'true' ">

    <Touch Files="$(MSBuildProjectDirectory)\Properties\Build\_buildforcer"
           AlwaysCreate="true"/>
</Target>

You can do this by means of an Import if you want. This forces the project to always build, not just when a project it depends on changes.

But hey, those dependent projects are often application projects, which are typically thin wrappers above business libraries. Moreover, a compilation isn't forced this way. It's just MSBuild executing all targets only to find out that everything is up to date. So, your F5 performance won't be substantially degraded.

I've explicitly marked the None item as Visible, since the item must be visible for this mechanism to work. On a related note, you might have to reload your solution after making the above changes.

Upvotes: 1

Kendall Frey
Kendall Frey

Reputation: 44316

To elaborate on Peter's answer, this is what I did.

Right click on the C# project in the Solution Explorer, and select Add > Existing Item.

Browse to the C++ output folder, select the DLL file, and instead of opening it with the Add button, click the dropdown button beside it, and select Add As Link.

What I initially did next was right click on the link that was added to your C# project, and select Properties, then change Copy to Output Directory to Copy if newer. But then I realized that will copy the Debug version (since that's where the link was pointing to), instead of the current configuration. So I left it at Do not copy, since my command line automatically selects the right configuration.

So now the C# project has a link to the C++ project output, which will trigger a rebuild whenever the C++ project changes, even though the link has nothing to do with the output of the project.

For the record, here's my post-build command line in the C# project (foobar is the C++ DLL).

copy "$(SolutionDir)\$(Configuration)\foobar.dll" "$(TargetDir)"

Upvotes: 5

Peter
Peter

Reputation: 46

If setting the project dependency inside your solution does not help just try to include the .dll from the C++ project as a link into the other project, then it should recognize that the file was changed and should rebuild the project.

Upvotes: 3

Related Questions