15

We are migrating our compilation system to msbuild, and we've found that some projects report the following error:

c:\src\libs\a_lib\A\A.vcxproj : error MSB4057: The target "C" does not exist in the project.

c:\src\libs\a_lib\B\B.vcxproj : error MSB4057: The target "C" does not exist in the project.

c:\src\libs\a_lib\C\C.vcxproj : error MSB4057: The target "C" does not exist in the project.

c:\src\libs\a_lib\D\D.vcxproj : error MSB4057: The target "C" does not exist in the project.

The compilation line is

msbuild "c:\src\libs\a_lib\a_lib.sln" /nologo "/target:C" /t:build "/p:Configuration=Release" "/p:Platform=Win32"

As can be seen, the solution has several projects. The project itself exists in the solution and can be compiled from within the VS IDE. Also, other targets do not fail (following the example: A, B, D).

Our previous compilation line worked correctly on the same project:

devenv "c:\src\libs\a_lib\a_lib.sln" /project "C" /build /nologo "Release|Win32"
Community
  • 1
  • 1
cbuchart
  • 10,847
  • 9
  • 53
  • 93

2 Answers2

24

The problem comes from the fact that such project is nested inside a solution folder (Tests in this example) in the Solution Explorer. Target name must include the name of such folders (Tests\C), so the correct compilation line is

msbuild "c:\src\libs\a_lib\a_lib.sln" /nologo "/target:Tests\C" /t:build "/p:Configuration=Release" "/p:Platform=Win32"
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • 5
    This is the answer that I needed, but with a caveat: The "folders" referenced here appear to be Solution folders, not disk folders. Studio makes you create your own Folder structure in the solution tree, separate from the actual disk-based folder structure. We probably all try to make the two mimic one another, but I had a folder that had a different name on-disk than it did in Solution Explorer. Renaming the Solution Explorer folder to match the disk name fixed the problem. Presumably using the Solution folder name instead of the disk name would have worked, too. – BRebey Feb 11 '20 at 16:08
  • 1
    @BRebey thanks for pointing this out. Indeed, in my case the folder was, as you mention, only a solution-folder (not a disk one). I've updated the answer to reflect that. – cbuchart Feb 11 '20 at 16:34
  • @cbuchart Thank you and it finally works. My project is inside a solution folder and must specifying with quotation marks! – Houcheng Nov 06 '20 at 09:37
  • 1
    Also note that you must use backslashes. Using forward slashes does not work. – Jan Jun 10 '21 at 09:42
  • I'm running into the same problem but I do not understand this solution. Where, What and Why. Confused as hell whats a "Solution" in this context and what on earth is "Disk" folder... Anyone able to simplify this? Thanks – ThisQRequiresASpecialist Sep 22 '22 at 13:09
  • 1
    @ThisQRequiresASpecialist For _solution folder_ I refer to the folders made to organize projects inside the VS solution, to distinguish from directories in the filesystem (_disk folders_). – cbuchart Sep 22 '22 at 15:01
1

As indicated by the other answer, the problem is related to a target project not being found by msbuild. Besides a wrong path there is another potential reason for that: multitargeting.
This happened to me in a non-SDK style project, when referencing a SDK style project, targeting both: net461 and netstandard2.0. In this case you may have to extend the project reference in the non-SDK style project by also defining the target framework of the project reference:

<ProjectReference Include="..\..\myProjRef.csproj">
  <Project>{d1b31534-48ae-428e-a174-b679fda90dde}</Project>
  <Name>MyProjRef</Name>
  <AdditionalProperties>TargetFramework=net461</AdditionalProperties>
</ProjectReference>

Note, the <AdditionalProperties> specified: TargetFramework=net461 leads to the specific target inside the MyProjRef project and removed the error.

Haymo Kutschbach
  • 3,322
  • 1
  • 17
  • 25