# Generating C# .NET6 Projects - Problems with DOTNET\_SDK

**URL:** https://discourse.cmake.org/t/generating-c-net6-projects-problems-with-dotnet-sdk/5745
**Category:** Code
**Tags:** os:windows, gen:vs, lang:csharp
**Created:** [May 27, 2022, 12:34am UTC](https://discourse.cmake.org/t/generating-c-net6-projects-problems-with-dotnet-sdk/5745 "2022-05-27T00:34:09Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mattsnowboard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mattsnowboard/32/2457_2.png) [@mattsnowboard](https://discourse.cmake.org/u/mattsnowboard)
#### Post date: [May 27, 2022, 12:34am UTC](https://discourse.cmake.org/t/generating-c-net6-projects-problems-with-dotnet-sdk/5745/1 "2022-05-27T00:34:09Z")

</div>

I have a project with both C/C++ native code, and C# .NET code. We can currently generate C# projects in cmake which use the “old” style of csproj files and it works perfectly fine for .NET 4.8. E.g. `set_target_properties(${this_target} PROPERTIES DOTNET_TARGET_FRAMEWORK_VERSION "v4.8")`

We get the csproj files like:

```xml
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PreferredToolArchitecture>x64</PreferredToolArchitecture>
  </PropertyGroup>
  <PropertyGroup Label="Globals">
...

```

rather than the “SDK” style csproj files:

```xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Label="Globals">
...

```

Now, we want to target .NET 6 instead.

If I try to just switch the framework (`set_target_properties(${this_target} PROPERTIES DOTNET_TARGET_FRAMEWORK "net6.0"`), I run into problems with NuGet restore and get a bunch of warnings in Visual Studio. Likely because I’m using the “old” style of csproj file, rather than the new “SDK” file.

I saw that CMake supports the SDK csproj files which seemed great!

```auto
set_target_properties(${this_target} PROPERTIES
    DOTNET_SDK "Microsoft.NET.Sdk"
    DOTNET_TARGET_FRAMEWORK "net6.0")

```

Unfortunately, this doesn’t support many other features that we also require. For example we currently use this

```auto
target_compile_options(${this_target} PUBLIC "/unsafe")
target_compile_options(${this_target} PUBLIC "/nowarn 7035")

```

With the old .NET, this generates some lines in the csproj like:

```xml
    <AdditionalOptions>"/nowarn 7035"</AdditionalOptions> <!-- note that visual studio normally uses code like <NoWarn>7035</NoWarn>, but this also works -->
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

```

With the `DOTNET_SDK` these are completely absent.

Similarly, we use this to target x64 rather than AnyCPU (which we need for calling into native DLL’s)

```auto
    set(CMAKE_CSharp_FLAGS "${CMAKE_CSharp_FLAGS} /platform:x64 /langversion:latest")

```

And previously that would generate:

```xml
    <PlatformTarget>x64</PlatformTarget>

```

That no longer works, which would be a problem for our project.

Finally, we use `add_custom_command` in our current .NET4.8 projects to copy native DLL’s from the C/C++ projects in order to p/invoke into those DLL’s. I understand that is not yet working according to the documentation, but I could not figure out another way to copy these DLL’s from a native target to the output of a given managed target without the custom command (though perhaps we can solve that with a dummy project, I’m willing to do a work-around like that unless there is a better solution).

So my question is, are there plans to bring the `DOTNET_SDK` option up to parity with the old csproj generator?

---

<div class="post-metadata">

### Author: ![mattsnowboard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mattsnowboard/32/2457_2.png) [@mattsnowboard](https://discourse.cmake.org/u/mattsnowboard)
#### Post date: [June 9, 2022, 6:36pm UTC](https://discourse.cmake.org/t/generating-c-net6-projects-problems-with-dotnet-sdk/5745/2 "2022-06-09T18:36:38Z")

</div>

In summary, `DOTNET_SDK` type projects are missing:

- A way to set PlatformTarget to x64
- Support for compiler options (like “unsafe” or other flags like “nowarn”)
- A way to copy additional files

Are there plans to address these gaps?

---

<div class="post-metadata">

### Author: ![mattsnowboard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mattsnowboard/32/2457_2.png) [@mattsnowboard](https://discourse.cmake.org/u/mattsnowboard)
#### Post date: [June 21, 2022, 3:55pm UTC](https://discourse.cmake.org/t/generating-c-net6-projects-problems-with-dotnet-sdk/5745/3 "2022-06-21T15:55:19Z")

</div>

Well I looked through the source code and found what I was missing. If I just set the target property `VS_GLOBAL_xxx yyy` it translates to `<xxx>yyy</xxx>` in the csproj file!

There are a few things I’ve seen in .csproj files that also have xml attributes, which I wouldn’t be able to set this way, but they are not blockers for me.

As for copying additional files without the custom commands, I’ve resorted to just making a dummy csproj that does the copying and adding it to the solution with `include_external_msproject`. That feels like a hack but I can live with it.

Would still like for some of the old behavior, like compiler options translating to csproj properties where possible, but this works for now.
