1

Similar questions have been addressed here, here, etc., but they all refer to problems with external references. In my solution I want to make a reference from one C# project to another, so the output of projectB is available in the output of projectA (Copy Local: yes). However, as ProjectB is listed in projectA dependencies, it's listed with a warning yellow triangle:

example picture

The first project in the "Projects" branch is a library, but the second project (the would be ProjectB) is a .Net console executable from which I don't require anything but the executable itself.

I'm guessing VS2019 is complaining because I'm not exporting anything from the ProjectB assembly, so nothing can be imported into ProjectA. The same behavior happens if ProjectB is a C++ project.

My question is: is there a better way to reference one project from another, so the referenced project is compiled and copied into the other project's output directory? (I mean, without resorting to ugly hacks like making ProjectB's output dir the same as ProjectA's or adding custom steps to ProjectB).

Please note that my current configuration works as intended, except for the annoying/misleading warning icon in the browser.

This is how ProjectB is referenced from ProjectA.csproj:

    <ProjectReference Include="..\ProjectB\ProjectB.csproj">
      <Private>true</Private>
    </ProjectReference>

EDIT: Added clarification about custom step.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
Guillermo Prandi
  • 1,537
  • 1
  • 16
  • 31
  • It’s not really necessary to blur out project names. Pretty sure no private or IP information can be gleamed by not doing so –  Sep 19 '20 at 04:12
  • @MickyD Since I'm using my real name competition might guess what I'm working on (I work in a very small niche in my country). – Guillermo Prandi Sep 19 '20 at 14:18

1 Answers1

0

Instead of adding the console application as a reference with Copy Local=Yes, and setting output path to ProjectA's, you should write a command for post-build event in Visual Studio:

  1. Go to ProjectB's properties
  2. Choose Build Events tab and find "Post-build event command line" textbox
  3. Write a copy command using relative paths and macros

Just as an example:

copy /Y "$(TargetDir)$(ProjectName).exe" "$(SolutionDir)ProjectA\Debug\bin\$(ProjectName).exe"

Update: if you don't want ProjectB to be aware of ProjectA (I can't think why), then add the steps to ProjectA. To make sure ProjectB is built before being copied to ProjectA's bin directory, either add a pre-build command to build ProjectB, or set correct build order and make sure you always build the entire solution before running ProjectA so latest changes in ProjectB's code are compiled and copied over.

Delphi.Boy
  • 1,199
  • 4
  • 17
  • 38
  • Thank you, but that's no what I need, since I don't want `ProjectB` to be aware of `ProjectA`. I'll make it clearer in the question. – Guillermo Prandi Sep 19 '20 at 14:20
  • Then add custom post-build/pre-build event to `ProjectA`. Edited my answer accordingly. – Delphi.Boy Sep 19 '20 at 20:07
  • The problem with pre-build/post-build is that the projects may have different output folders because they could have different architectures, release/debug build configurations, etc. I certainly can open a CMD box and copy the files manually even, but I was 1) hoping that the build environment would handle that automatically (given that it does everything I want except for the warning overlay) and 2) hoping for an explanation as for why does the warning overlay shows up in the first place. Notice that my question is "is there a better way to **reference** one project from another?" – Guillermo Prandi Sep 19 '20 at 22:16