无法通过 dotnet build 命令编译模板 C++/CLI (.NET Core 3.1) 项目 [英] Cannot compile template C++/CLI (.NET Core 3.1) project via dotnet build command
问题描述
- 安装最新的稳定版 Visual Studio 2019 16.4.2(检查 .NET Core Development 和 Desktop development with C++ 工作负载,还要确保C++/CLI 支持 组件已选中).
- 使用 CLR 类库 (.NET Core) 创建新项目模板(或 CLR 空项目 (.NET Core),如果您愿意).
- 通过 dotnet build 命令编译将失败(dotnet build CLRNetCoreTest.sln/p:Configuration=Debug/p:Platform=x86):
- 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):
通过 msbuild 命令编译成功 ("%ProgramFiles(x86)%Microsoft Visual Studio2019EnterpriseMSBuildCurrentBinMSBuild.exe" CLRNetCoreTest.sln/p:Configuration=Debug/p:Platform=x86).
Compilation via msbuild command is successfull ("%ProgramFiles(x86)%Microsoft Visual Studio2019EnterpriseMSBuildCurrentBinMSBuild.exe" CLRNetCoreTest.sln /p:Configuration=Debug /p:Platform=x86).
这很奇怪 - 我认为 dotnet 是构建在 msbuild 之上的包装器..
And that is very strange - I supposed dotnet to be a wrapper built on the top of msbuild..
示例项目附加(运行 Build.bat 进行编译).
Sample project is attached (run Build.bat for compilation).
推荐答案
添加更多细节:
这很奇怪 - 我认为 dotnet 是一个基于msbuild 的顶部.
And that is very strange - I supposed dotnet to be a wrapper built on the top of msbuild.
同意Martin,dotnet build
目前不支持构建C++项目,因为dotnet cli不知道在哪里可以找到必要的C++ xx.targets
和 xx.props
.
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
.
你的项目是一个C++/CLR项目(xx.vcxproj),目标框架是.net core
,和实际的.net core
项目(xx.csproj 或 xx.vbproj),xx.vcxproj
导入了许多 C++ 特定文件,这些文件未包含在 .net core sdk
中.
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
.
例如:Go C:Program Filesdotnetsdkxxx
你可以在那里找到很多 msbuild 目标和道具文件,但是你找不到 Microsoft.Cpp.Default.props 文件.此文件来自 C++ 工作负载,而不是 .net 核心 SDK.
For example: Go C:Program Filesdotnetsdkxxx
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.
所以虽然 dotnet build
使用 msbuild 来构建项目,但它实际上并没有使用 VS 文件夹中的相同 msbuild.exe(C:Program Files (x86)Microsoft Visual StudioxxxxxxMSBuild1xxxBinMSBuild.exe
).并且只有来自 VS IDE 或 MSBuild 构建工具包的 msbuild.exe 可用于构建此项目.
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 StudioxxxxxxMSBuild1xxxBinMSBuild.exe
). And only the msbuild.exe from VS IDE or MSBuild build tools package can be used to build this project.
另外:
如果您的最终目标是在远程服务器上构建项目,那么您可能正在寻找 Build Tools for Visual Studio 2019` 包.(如果您获得了相应的 lisense,则免费,更多详细信息参见 this) 它不需要安装 VS IDE.(下载链接此处)
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)
这篇关于无法通过 dotnet build 命令编译模板 C++/CLI (.NET Core 3.1) 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!