Search code examples
msbuilddependenciesprojects-and-solutions

When to use <ProjectReference> in project files?


Summary:

Projects build in wrong order with visual studio and managed C++ and C# projects

Description:

I have a massive (100+ projects) solution file that is building a few projects in the wrong order. The solution file contains the following types of projects:

  • native C/C++
  • Managed C++
  • Managed C#

The solution contains all the proper dependencies between the different types of projects. Ok, so when I build from the command line (Using MSBuild), there is a problem. The dependencies for the managed projects (both C++ and C#) get built in the wrong order. For instance a project will fail to build because a managed dependency is missing. For instance a managed C++ file will have a using declaration that will fail:

#using <foo.dll>

since foo.dll doesn't exist yet.

Which means that foo.dll should have been built before, but wasn't. Like I mentioned earlier, the dependencies are properly set up in the solution file. For instance, if foo depends on baz, I have this in the solution file...

Project("{C4ABA494-43D0-400A-9917-20E167A12CFD}") = "Foo", "...\Foo.vcxproj", "{5A42640E-9E0A-442B-8A40-AA91AD5444AC}"
    ProjectSection(ProjectDependencies) = postProject
        ...
        {2CE32AE0-B129-40BA-B06E-A628FA149AB3} = {2CE32AE0-B129-40BA-B06E-A628FA149AB3}
    EndProjectSection
EndProject
...
Project("{C4ABA494-43D0-400A-9917-20E167A12CFD}") = "baz", "...\baz.csproj", "{2CE32AE0-B129-40BA-B06E-A628FA149AB3}"
    ProjectSection(ProjectDependencies) = postProject
        ...
    EndProjectSection
EndProject

So the solution file correctly has the dependency. But the dependency in the Foo.vcxproj project is only expressed by the #using directive. I've read on the visual studio blog that there is a known bug in ordering projects in msbuild. http://blogs.msdn.com/b/visualstudio/archive/2010/12/21/incorrect-solution-build-ordering-when-using-msbuild-exe.aspx

Their work around is to add an item called to my projects, like this:

<ProjectReference Include="... foo.csproj"> 
    <ReferenceOutputAssembly>false</ReferenceOutputAssembly> 
</ProjectReference>

Anyways, my question is: do I need to do this ONLY for my managed C++ projects? Or do I do this for Managed C++ AND C# projects? (I kind of believe I don't need to do this for C# projects since their dependencies are explicit)

Note: I have tried putting this on ALL projects in my build, and it didn't work so hot, as I got lots of strange build errors in my native projects...

Thanks for any response to this.


Solution

  • I had the same issue, but with C# projects only. It seems like MsBuild is NOT using solution file dependencies. It is using project references inside the project files to create build order. Try to update all your ProjectReferences to get correct build order. In your case you have to add managed project reference (dependency) into your C++ project file.

    The answer to your question is: Yes, you have to do it for both Managed C++ AND C# projects. Setting dependencies inside sln file is not enough if you are building with MSBuild.