如何使用CMake使用.NET Standard目标框架生成Visual Studio项目 [英] How to generate Visual Studio project with .NET Standard target framework with CMake

查看:179
本文介绍了如何使用CMake使用.NET Standard目标框架生成Visual Studio项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CMake生成Visual Studio C#项目。这是我到目前为止所拥有的:

I am trying to generate a Visual Studio C# project with CMake. This is what I have so far:

cmake_minimum_required(VERSION 3.10)

project(myProject VERSION 0.1.0 LANGUAGES CSharp)

add_library(myLib SHARED
    src/file1.cs
    src/file2.cs
    src/file3.cs)

set_property(TARGET myLib PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "netstandard1.4")

我从我用Visual Studio生成的.csproj文件看起来像这样:

I got the framework name from a .csproj file I generated with Visual Studio that looks like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>
</Project>

这是构建.NET Standard 1.4库所需的。但是,CMake会生成此代码(以及其他所有内容):

This is what I need in order to build the library for .NET Standard 1.4. However, CMake generates this (among all the other stuff):

<TargetFrameworkVersion>netstandard1.4</TargetFrameworkVersion>

这似乎无效,因为Visual Studio抱怨它无法打开项目。我在CMake文档中找不到设置< TargetFramework> 而不是< TargetFrameworkVersion> 的任何内容。

This does not seem to be valid, since Visual Studio complains that it cannot open the project. I did not find anything in the CMake documentation to set <TargetFramework> instead of <TargetFrameworkVersion>. Is this possible?

推荐答案

这是我设法正确生成项目的方式。当心,这是一个丑陋的hack。 在此之前,请检查CMake是否已正确更新以支持此

This is how I managed to generate the project properly. Beware, it is an ugly hack. Check whether CMake has been updated to properly support this before you go this way.

因此,第一件事:不需要 VS_DOTNET_TARGET_FRAMEWORK_VERSION 。要添加适当的< TargetFramework> ,可以使用此行:

So, first thing: The VS_DOTNET_TARGET_FRAMEWORK_VERSION is not necessary. To add the proper <TargetFramework>, this line can be used:

set_property(TARGET myLib PROPERTY VS_GLOBAL_TargetFramework "netstandard1.4")

您还想添加它,因为出于某种原因,CMake认为默认情况下以C#3作为语言级别生成一个项目是一个好主意:

You also want to add this, because for reasons that escape me, CMake thinks it is a good idea to generate a project with C# 3 as language level by default:

set_property(TARGET myLib PROPERTY VS_GLOBAL_LangVersion "6")

生成的项目无法在Visual Studio中正确打开。这是因为.NET Core对CMake当前无法生成的csproj格式进行了一些更改。作为参考,本文涵盖了它们。 CMake无法正确生成它们,因此,我将首先告诉您您需要进行哪些修改,然后为您提供一些可修改的PowerShell脚本。

The generated project will not open in Visual Studio properly. That is because with .NET Core, there came some changes to the csproj format that CMake is currently unable to produce. For reference, this article covers them. CMake is unable to properly generate them, so I will first tell you what modifications you would need to make and then give you some hacky PowerShell script that does it for you.

在根< Project>上有一个属性 Sdk 现在,我们需要这样设置:

There is an attribute Sdk on the root <Project> now, which we need to set like this:

<Project Sdk="Microsoft.NET.Sdk" ...

自动生成的 ZERO_CHECK 项目无法正常工作,所以让我们摆脱它(我们真的不需要它)。从< ProjectReference ...< / ProjectReference> 删除它,如果它是唯一项,则删除整个XML标签。

The automatically generated ZERO_CHECK project does not work, so let's just get rid of it (we don't need it really). Delete it from <ProjectReference ... </ProjectReference> and if it's the only item, delete the whole XML tag.

然后,由于该项目现在将自动引用SDK中的内容,因此我们需要删除对 Microsoft.CSharp.targets 和<$ c的引用$ c> Microsoft.Common.props 。否则,Visual Studio将向您显示警告和错误,提示您加载了多次内容。只需删除包含这些字符串的行即可。

Then, since the project will now automatically reference stuff from the SDK, we need to get rid of the references to Microsoft.CSharp.targets and Microsoft.Common.props. Otherwise, Visual Studio will show you a warning and an error that stuff is loaded multiple times. Just delete the lines that contain these strings.

标准项目 ALL_BUILD 和 ZERO_CHECK 不起作用,我也不用费心去适当地修复它们,因为它们并不是真正必要的。我只是从 .sln 文件中删除了它们。

The standard projects ALL_BUILD and ZERO_CHECK which CMake always generates do not work and I could not be bothered to properly fix them, since they are not really necessary. I just deleted them from the .sln file.

就是这样!现在您可以在Visual Studio中打开该项目,它将正常运行。

That's it! Now you can open the project in Visual Studio and it will work properly.

这是一个我写为解决方法的脚本,可正确生成项目&解。它在 cmake-vs 文件夹中生成解决方案。您需要.NET Core SDK中的 dotnet 实用工具。

This is a script I wrote as workaround to properly generate the project & solution. It generates the solution in a folder cmake-vs. You need the dotnet utility which is part of the .NET Core SDK.

请注意,路径可能不是便携式的。将 myLib 替换为您的库名。

Be aware that paths may not be portable. Replace myLib with your library name.

mkdir -Force cmake-vs | Out-Null
Set-Location cmake-vs
& "$Env:Programfiles\CMake\bin\cmake.exe" -G "Visual Studio 15 2017 Win64" ".."
& "$Env:Programfiles\CMake\bin\cmake.exe" --build .
((Get-Content "myLib.csproj") `
  <# add Sdk attribute to project which is needed by netstandard1.4 #> `
  -replace ('<Project ','<Project Sdk="Microsoft.NET.Sdk" ') `
  <# remove reference to ZERO_CHECK project which causes problems #> `
  -replace ('(?ms)<ProjectReference.*</ProjectReference>','') |
  <# remove imports that are unneccessary for netstandard1.4 and cause
     problems #> `
  Select-String -Pattern "Microsoft.CSharp.targets" -NotMatch |
  Select-String -Pattern "Microsoft.Common.props" -NotMatch |
  <# for some reason, Select-String prepends an empty line which is not
     allowed before <?xml ..., so we trim it away. #>
  Out-String).Trim() | Out-File "myLib.csproj"
<# use dotnet util (part of the .NET Core SDK) to remove projects with problems
   from the solution because I am really tired of regexes #>
& "$Env:Programfiles\dotnet\dotnet.exe" sln myLib.sln remove ALL_BUILD.vcxproj
& "$Env:Programfiles\dotnet\dotnet.exe" sln myLib.sln remove ZERO_CHECK.vcxproj

这篇关于如何使用CMake使用.NET Standard目标框架生成Visual Studio项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆