.NETStandard compilation with CMake

I have an existing C# project which is targetted for .net48 and .netstandard2.0. It works fine when I compile it separately. I had to work on moving this project to CMake based project and I am encountering issues of the project containing references to compile tag with .NET Framework. This results in problems where finally, when the build starts for the generated csproj file from CMake, I get into errors mentioning some of the referenced packages are unavailable for .net40. Example CMakeLists.txt file:

cmake_minimum_required(VERSION 3.29)
project(MyProjectDNC CSharp)
# Add your C# source files
file(GLOB_RECURSE CS_FILES "*.cs")
add_library(MyProjectDNC  SHARED ${CS_FILES})
set_target_properties(MyProjectDNC PROPERTIES
    DOTNET_TARGET_FRAMEWORK "netstandard2.0"
)
set_target_properties(MyProjectDNC PROPERTIES
    COMPILE_DEFINITIONS "TRACE;;NETSTANDARD;NETSTANDARD2_0"
)
set_property(TARGET MyProjectD
    PROPERTY VS_PACKAGE_REFERENCES "abc.xyz_23.2.*;Microsoft.Windows.Compatibility_2.0.1;pqr.lmn_23.2.*;Newtonsoft.Json_13.0.1;System.Configuration.ConfigurationManager_4.7.0;System.Threading.Channels_7.0.0"
)

Generated csproj file contains the targetframework part correct:

<ProjectName>MyProject </ProjectName>
<TargetFramework>netstandard2.0</TargetFramework>

But it contains following strange lines:

  <Compile Include="C:\Users\abc\source\repos\MyController\src\MyProject\MyProjectDNC\out\build\x64-Debug\CMakeFiles\3.29.3\CompilerIdCSharp\obj\x64\Debug\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs">
    <Link>out\build\x64-Debug\CMakeFiles\3.29.3\CompilerIdCSharp\obj\x64\Debug\.NETFramework,Version=v4.7.2.AssemblyAttributes.cs</Link>
  </Compile>
  <Compile Include="C:\Users\abc\source\repos\MyController\src\MyProject\MyProjectDNC\out\build\x64-Debug\obj\x64\Debug\.NETFramework,Version=v4.0.AssemblyAttributes.cs">
    <Link>out\build\x64-Debug\obj\x64\Debug\.NETFramework,Version=v4.0.AssemblyAttributes.cs</Link>
  </Compile>

Following are the error seen in building the complete project:

C:\Users\abc\source\repos\MyController\src\MyProject\MyProjectDNC\out\build\x64-Debug\MyProjectDNC.csproj : error NU1202: Package abc.xyz 23.2.347.1 is not compatible with net40 (.NETFramework,Version=v4.0). Package abc.xyz 23.2.347.1 supports: net48 (.NETFramework,Version=v4.8) 
C:\Users\abc\source\repos\MyController\src\MyProject\MyProjectDNC\out\build\x64-Debug\MyProjectDNC.csproj : error NU1202: Package pqr.lmn 23.2.347.1 is not compatible with net40 (.NETFramework,Version=v4.0). Package pqr.lmn 23.2.347.1 supports: net48 (.NETFramework,Version=v4.8) 

Any help on this is much appreciated.

Looks like a duplicate of your other topic.

Anyway, setting DOTNET_TARGET_FRAMEWORK might help.

I thought my other topic was mainly associated with the nuget package references. Here even after setting nuget package reference and DOTNET_TARGET_FRAMEWORK its not working. You can check the CMakeLists.txt file above.

Ah, sorry, I haven’t noticed that you have already set the DOTNET_TARGET_FRAMEWORK property.
Then maybe setting the “global” CMAKE_DOTNET_TARGET_FRAMEWORK value would make a difference (not very likely though).

I only have .NET 8 installed, so I tried to generate a project targeting that version, here’s an example.
With that project I get the following .csproj generated:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
    ...
    <TargetFramework>net8.0</TargetFramework>
    ...
  </PropertyGroup>
  ...
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  </ItemGroup>
  ...
</Project>

There are no references to v4.x anywhere, and the project builds and runs just fine:

> mkdir build
> cd build
> cmake -G "Visual Studio 17 2022" ..
> cmake --build . --config Release

> Release\some.exe
Some: value

So I can’t say for sure what could be wrong in your case. Perhaps something isn’t right with the installed .NET SDK/runtime version(s)? Or some of the referenced packages only support “classic” .NET Framework (or the other way around).