C# – Force Visual Studio to rebuild dependent projects

cprojects-and-solutionsvisual studiovisual studio 2012

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.

Best Answer

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)"