无法在启用了.NET 7.0的CLR支持的情况下,在C++ DLL中引用.NET 7.0的C#项目。

huangapple go评论58阅读模式
英文:

Can't reference .net 7.0 c# project in c++ dll with CLR support enabled for .net 7.0

问题

我有一个C++的DLL,原本支持.NET Framework 4.8的CLR,我正在将这个库升级到引用.NET 7.0,因此尝试将这个C++的DLL的CLR支持也升级为.NET 7.0,但是出现了以下警告:

项目 'projectName' 目标为 'net7.0'。它不能被目标为 '.NETStandard,Version=v2.0' 的项目引用。

我尝试通过项目设置GUI和.vcxproj文件进行更新,但都没有成功。我将平台工具集版本升级到了VS2022(v143),将CLR支持选项从'Common Language Runtime Support'更改为'.NET Core Runtime Support',然后点击应用,将.NET Core目标框架设置为v7.0。然后构建项目时出现上述错误。

上述更改导致我的全局和特定构建配置的vcxproj详细信息如下:

<PropertyGroup Label="Globals">
    <ProjectGuid>{78942B09-0F2D-49D7-A329-1363F20188A5}</ProjectGuid>
    <Keyword>Win32Proj</Keyword>
    <RootNamespace>projectName</RootNamespace>
    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
    <TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
    <DependsOnNETStandard>false</DependsOnNETStandard>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseDebugLibraries>true</UseDebugLibraries>
    <PlatformToolset>v143</PlatformToolset>
    <CharacterSet>Unicode</CharacterSet>
    <CLRSupport>NetCore</CLRSupport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    <ConfigurationType>DynamicLibrary</ConfigurationType>
    <UseDebugLibraries>false</UseDebugLibraries>
    <PlatformToolset>v143</PlatformToolset>
    <WholeProgramOptimization>true</WholeProgramOptimization>
    <CharacterSet>Unicode</CharacterSet>
    <CLRSupport>NetCore</CLRSupport>
  </PropertyGroup>

我保留了其他项的默认设置,比如运行时标识符和Windows目标平台,尽管我尝试从vcxproj中删除这些设置,但仍然看到相同的错误。我还尝试了使用.NET 5.0作为两个项目的目标,但仍然出现了相同的错误(提到.NET 5而不是.NET 7)。

一个以.NET 7.0为目标的C++ CLR DLL是否可以与一个.NET 7.0的C#项目一起使用?我可能漏掉了什么?

英文:

I have a c++ dll which originally had CLR support for .net framework 4.8, I am updating the library this references to .net 7.0, and so have tried to update the CLR support for this c++ dll to also target .net 7.0, however this results in the warning:

Project &#39;projectName&#39; targets &#39;net7.0&#39;. It cannot be referenced by a project that targets &#39;.NETStandard,Version=v2.0&#39;

I have tried updating through the project settings GUI and in the .vcxproj file with no luck, I updated the platform toolset version to VS2022 (v143), the CLR support option from 'Common Language Runtime Support' to '.NET Core Runtime Support', hit apply and then set the .Net Core Target Framework to v7.0. Then building the project see's the above error show.

The above changes resulted in this for my global and build config specific vcxproj details:

&lt;PropertyGroup Label=&quot;Globals&quot;&gt;
    &lt;ProjectGuid&gt;{78942B09-0F2D-49D7-A329-1363F20188A5}&lt;/ProjectGuid&gt;
    &lt;Keyword&gt;Win32Proj&lt;/Keyword&gt;
    &lt;RootNamespace&gt;projectName&lt;/RootNamespace&gt;
    &lt;WindowsTargetPlatformVersion&gt;8.1&lt;/WindowsTargetPlatformVersion&gt;
    &lt;TargetFrameworkVersion&gt;v7.0&lt;/TargetFrameworkVersion&gt;
    &lt;RuntimeIdentifier&gt;win7-x64&lt;/RuntimeIdentifier&gt;
    &lt;DependsOnNETStandard&gt;false&lt;/DependsOnNETStandard&gt;
    &lt;TargetFramework&gt;net7.0&lt;/TargetFramework&gt;
  &lt;/PropertyGroup&gt;
  &lt;Import Project=&quot;$(VCTargetsPath)\Microsoft.Cpp.Default.props&quot; /&gt;
  &lt;PropertyGroup Condition=&quot;&#39;$(Configuration)|$(Platform)&#39;==&#39;Debug|x64&#39;&quot; Label=&quot;Configuration&quot;&gt;
    &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
    &lt;UseDebugLibraries&gt;true&lt;/UseDebugLibraries&gt;
    &lt;PlatformToolset&gt;v143&lt;/PlatformToolset&gt;
    &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;CLRSupport&gt;NetCore&lt;/CLRSupport&gt;
  &lt;/PropertyGroup&gt;
  &lt;PropertyGroup Condition=&quot;&#39;$(Configuration)|$(Platform)&#39;==&#39;Release|x64&#39;&quot; Label=&quot;Configuration&quot;&gt;
    &lt;ConfigurationType&gt;DynamicLibrary&lt;/ConfigurationType&gt;
    &lt;UseDebugLibraries&gt;false&lt;/UseDebugLibraries&gt;
    &lt;PlatformToolset&gt;v143&lt;/PlatformToolset&gt;
    &lt;WholeProgramOptimization&gt;true&lt;/WholeProgramOptimization&gt;
    &lt;CharacterSet&gt;Unicode&lt;/CharacterSet&gt;
    &lt;CLRSupport&gt;NetCore&lt;/CLRSupport&gt;
  &lt;/PropertyGroup&gt;

I have left other items as default, such as runtime identifier and windows target platform, though I did try removing these from the vcxproj but saw the same error. I have also tried .net 5.0 for both projects, with the same error (mentioning .net 5 instead)

Can a c++ clr dll targeting .net 7.0 cpr support, use a .net 7 c# project? What might I be missing?

答案1

得分: 1

原来我们有一个名为 'Directory.Build.props' 的文件,它将目标框架版本设置为 .netstandard2.0,将其更新为 .net7.0 解决了警告。

英文:

Turns out we had a 'Directory.Build.props' file which was setting a target framework version to .netstandard2.0, updating this to .net7.0 resolved the warning

huangapple
  • 本文由 发表于 2023年7月10日 17:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定