Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compile template C++/CLI (.NET Core 3.1) project via dotnet build command

  1. Install latest stable Visual Studio 2019 16.4.2 (check .NET Core Development and Desktop development with C++ workloads, also make sure that C++/CLI support component in checked).
  2. Create new project with CLR Class Library (.NET Core) template (or CLR Empty Project (.NET Core) if you like).
  3. Compilation via dotnet build command will fail (dotnet build CLRNetCoreTest.sln /p:Configuration=Debug /p:Platform=x86): enter image description here

Compilation via msbuild command is successfull ("%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe" CLRNetCoreTest.sln /p:Configuration=Debug /p:Platform=x86).

And that is very strange - I supposed dotnet to be a wrapper built on the top of msbuild..

Sample project is attached (run Build.bat for compilation).

like image 683
bairog Avatar asked Jan 14 '20 05:01

bairog


2 Answers

To add more details:

And that is very strange - I supposed dotnet to be a wrapper built on the top of msbuild.

Agree with Martin, dotnet build for now doesn't support building C++ project, cause dotnet cli doesn't know where to find the necessary C++ xx.targets and xx.props.

Your project is a C++/CLR project(xx.vcxproj) whose target framework is .net core, it has big difference with actual .net core projects(xx.csproj or xx.vbproj), the xx.vcxproj imports many C++ specific files which is not included in .net core sdk.

For example: Go C:\Program Files\dotnet\sdk\xxx you can find many msbuild targets and props files there, but you can't find Microsoft.Cpp.Default.props file. This file comes from C++ workload instead of .net core SDK.

So though dotnet build uses msbuild to build project, it actually doesn't use the same msbuild.exe from VS folder(C:\Program Files (x86)\Microsoft Visual Studio\xxx\xxx\MSBuild\1xxx\Bin\MSBuild.exe). And only the msbuild.exe from VS IDE or MSBuild build tools package can be used to build this project.

In addition:

If your final goal is to build the project in remote server, maybe you're looking for Build Tools for Visual Studio 2019` package.(Free if you've gotten corresponding lisense, more details see this) It doesn't require VS IDE to be installed. (Download link here)

like image 149
LoLance Avatar answered Sep 29 '22 04:09

LoLance


C++ projects can only be built with the msbuild.exe shipped with Visual Studio as it relies on Visual Studio's C++ tooling.

The cross-platform dotnet command with its cross-platform MSBuild distribution does not have these tools and as such unable to compile C++ projects. The dotnet command does not use the visual studio version of MSBuild.

like image 26
Martin Ullrich Avatar answered Sep 29 '22 03:09

Martin Ullrich