MSBuild: error MSB4057: The target does not exist in the project
Asked Answered
A

3

17

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"
Auditorium answered 30/7, 2018 at 14:38 Comment(0)
A
25

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"
Auditorium answered 30/7, 2018 at 14:38 Comment(6)
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.Barrister
@Barrister 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.Auditorium
@Auditorium Thank you and it finally works. My project is inside a solution folder and must specifying with quotation marks!Splashdown
Also note that you must use backslashes. Using forward slashes does not work.Nunatak
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? ThanksDredge
@Dredge 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).Auditorium
S
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.

Subgenus answered 13/6, 2021 at 10:25 Comment(0)
G
0

Run dotnet restore from the terminal before the build.

Graphite answered 3/6, 2024 at 18:39 Comment(1)
Why do you believe this will solve the problem? How does this compare to the accepted answer, which proposes a very different approach?Hernardo

© 2022 - 2025 — McMap. All rights reserved.