问题
- 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).
- Create new project with CLR Class Library (.NET Core) template (or CLR Empty Project (.NET Core) if you like).
- Compilation via dotnet build command will fail (dotnet build CLRNetCoreTest.sln /p:Configuration=Debug /p:Platform=x86):
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).
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/59727986/cannot-compile-template-c-cli-net-core-3-1-project-via-dotnet-build-command