# .NETStandard compilation with CMake

**URL:** https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052
**Category:** Code
**Tags:** os:linux, os:windows
**Created:** [June 19, 2024, 9:22am UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052 "2024-06-19T09:22:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sameer\_Joshi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sameer_joshi/32/4673_2.png) [@Sameer\_Joshi](https://discourse.cmake.org/u/Sameer_Joshi)
#### Post date: [June 19, 2024, 9:22am UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052/1 "2024-06-19T09:22:54Z")

</div>

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:

```auto
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:

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

```

But it contains following strange lines:

```auto
  <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:

```auto
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.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [June 19, 2024, 9:27am UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052/2 "2024-06-19T09:27:09Z")

</div>

Looks like a duplicate of [your other topic](https://discourse.cmake.org/t/cmake-for-c-project-with-nuget-package-references/11028).

Anyway, setting [DOTNET\_TARGET\_FRAMEWORK](https://cmake.org/cmake/help/latest/prop_tgt/DOTNET_TARGET_FRAMEWORK.html) might help.

---

<div class="post-metadata">

### Author: ![Sameer\_Joshi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sameer_joshi/32/4673_2.png) [@Sameer\_Joshi](https://discourse.cmake.org/u/Sameer_Joshi)
#### Post date: [June 19, 2024, 9:30am UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052/3 "2024-06-19T09:30:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [June 19, 2024, 10:17am UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052/4 "2024-06-19T10:17:40Z")

</div>

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](https://cmake.org/cmake/help/latest/variable/CMAKE_DOTNET_TARGET_FRAMEWORK.html) value would make a difference (_not very likely though_).

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [June 19, 2024, 1:32pm UTC](https://discourse.cmake.org/t/netstandard-compilation-with-cmake/11052/5 "2024-06-19T13:32:06Z")

</div>

I only have .NET 8 installed, so I tried to generate a project targeting that version, here’s [an example](https://github.com/retifrav/MREs/tree/master/discourse-cmake-org/11052).  
With that project I get the following `.csproj` generated:

```xml
<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:

```cmd
> 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_).
