How to reference a project out of solution?

浪子不回头ぞ 提交于 2021-01-28 03:51:46

问题


I have a Visual Studio C# solution which consists of some projects. One of the projects needs to reference another project which is not part of the solution.

At the beginning I was referencing dlls:

<ItemGroup>
  <Reference Include="ExternalProj1, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\Proj1\ExternalProj1.dll</HintPath>
  </Reference>
</ItemGroup>

However I must reference projects so that those will generate their dlls. In fact If I reference dlls and they have not been created, I need to build those projects separately.

However when referencing projects:

<ItemGroup>
  <ProjectReference Include="..\..\Proj1\ExternalProj1">
    <Project>{3341b552-a569-4313-aabc-34452fff60ac}</Project>
    <Name>ExternalProj1</Name>
  </ProjectReference>
</ItemGroup>

However when building the compiler cannot find those assemblies. The strange thing is that building process is reported as completed successfully but the error window reports one warning:

The referenced component ExternalProj could not be found.

So, what am I doing wrong? Thank you


回答1:


I see you are using ProjectReference, which is what I'm familiar with in plain (non-NET) C++ projects. The Include attribute needs to name the file, not just the base name; e.g.

<ProjectReference Include="..\..\Proj1\ExternalProj1.vcxproj">

That is, ProjectReference is not Reference. See Common MSBuild Project Items

Also, the metadata that determines whether to link the LIB automatically is determined via the supplied props files if it is not specified for that item. Is a managed project even producing a LIB? So this should (with the filename correct) cause the nominated project to be built also as a dependent, doing something with its products is another issue altogether.

Try building from MSBuild.exe command line, not the IDE, to see the pure behavior before the IDE messes things up or adds more issues to figure out. And, feed it the specific proj file you are wanting, not the "solution" file. The .sln file is a strange beast and not only is it possible to have project references not present in the sln, there is no inherent concept of a sln file at all. Other than a list of projects to show in the IDE, it is a magic file converted into a master proj on the fly that lets you name various targets individually without having to know which proj file (or the path to it) which is handy enough, but mainly there for compatibility with VSBuild according to The Books. So avoid it, at least to simplify things to get the behavior you want during the exploration stage. Then add any complications back in if you still want them :) .



来源:https://stackoverflow.com/questions/26314094/how-to-reference-a-project-out-of-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!