Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
like image 882
cbuchart Avatar asked Jul 30 '18 14:07

cbuchart


2 Answers

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"
like image 154
cbuchart Avatar answered Nov 04 '22 05:11

cbuchart


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.

like image 1
Haymo Kutschbach Avatar answered Nov 04 '22 04:11

Haymo Kutschbach