Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 689
C Johnson Avatar asked Jan 03 '12 18:01

C Johnson


People also ask

What is the difference between project reference and DLL reference?

If you directly add a DLL then you are locked into whatever that particular DLL was built as. The project reference allows this to be a build time decision. This is correct what you are saying. At the same time it is not causing issues when working with .

What does a Csproj file do?

CSPROJ files define a project's content, platform requirements, versioning information, and web server or database server settings. They also list the files that are part of the project.

How do you add a reference to a project?

Add a reference In Solution Explorer, right-click on the References or Dependencies node and choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference. (You can right-click the project node and select Add from the fly-out menu to choose from these options, too.)

What are references in a project?

A reference is essentially an entry in a project file that contains the information that Visual Studio needs to locate the component or the service.


1 Answers

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.

like image 67
Ludwo Avatar answered Oct 01 '22 02:10

Ludwo